Skip to content

From an Existing Database

Working on a legacy project? Point the generator at the database and get a complete, tested, documented API for every table, without retyping a single schema.

Usage

bash
php artisan make:fullapi --from-database
bash
php artisan make:fullapi --from-database --tables=products,orders
bash
php artisan make:fullapi --from-database --with-migrations

The first form converts every user table; system tables are skipped automatically. --tables= narrows the run to the tables you list, and --with-migrations also writes the migration files, which is useful to version a hand-built database.

What the introspection detects

The introspection reads far more than column names:

  • Columns with their types and nullability, mapped to validation rules, casts, factories, DTO types and the model PHPDoc. A VARCHAR(255) NOT NULL UNIQUE becomes required|string|max:255|unique:... plus a unique factory value.
  • Foreign keys (real constraints on Laravel 11+, plus the <table>_id naming convention) become belongsTo relations, with the inverse hasMany on the parent model: both sides typed in the PHPDoc.
  • Pivot tables (two foreign keys, nothing else) become belongsToMany on both models instead of a useless intermediate entity.
  • Polymorphic pairs: commentable_type + commentable_id columns are detected as a proper morphTo relation.
  • Enum columns become native PHP backed enums with casts and Rule::enum() validation.
  • deleted_at enables soft deletes (trait, restore/force-delete endpoints).

Safety defaults

  • Migrations are not regenerated by default: the tables already exist. Pass --with-migrations when you want them as code-of-record.
  • The users table is skipped so your customized app/Models/User.php is never overwritten. Pass --tables=users explicitly if you really want it.

Inspecting without generating

The api-generator:introspect command emits the schema as JSON, so any tooling can build on top of it. Run it bare to list every user table (migrations, sessions and personal_access_tokens are filtered out), or point it at a table to get its column names, normalized types and soft-deletes flag:

bash
php artisan api-generator:introspect
bash
php artisan api-generator:introspect --table=products

This powers the Import from Database feature of the VS Code extension.

The payoff

Legacy database at 9:00, documented and tested REST API at 9:15:

bash
php artisan make:fullapi --from-database --tables=posts,categories,comments --pest --postman
php artisan test
php artisan serve

The test suite passes as generated, and if Scramble is installed the interactive documentation is already live at /docs/api.