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
php artisan make:fullapi --from-databasephp artisan make:fullapi --from-database --tables=products,ordersphp artisan make:fullapi --from-database --with-migrationsThe 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 UNIQUEbecomesrequired|string|max:255|unique:...plus a unique factory value. - Foreign keys (real constraints on Laravel 11+, plus the
<table>_idnaming convention) becomebelongsTorelations, with the inversehasManyon the parent model: both sides typed in the PHPDoc. - Pivot tables (two foreign keys, nothing else) become
belongsToManyon both models instead of a useless intermediate entity. - Polymorphic pairs:
commentable_type+commentable_idcolumns are detected as a propermorphTorelation. - Enum columns become native PHP backed enums with casts and
Rule::enum()validation. deleted_atenables soft deletes (trait, restore/force-delete endpoints).
Safety defaults
- Migrations are not regenerated by default: the tables already exist. Pass
--with-migrationswhen you want them as code-of-record. - The
userstable is skipped so your customizedapp/Models/User.phpis never overwritten. Pass--tables=usersexplicitly 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:
php artisan api-generator:introspectphp artisan api-generator:introspect --table=productsThis 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:
php artisan make:fullapi --from-database --tables=posts,categories,comments --pest --postman
php artisan test
php artisan serveThe test suite passes as generated, and if Scramble is installed the interactive documentation is already live at /docs/api.
