Join my Alpine.js course on Udemy for free 👀

Make Laravel model with migration, controller and factory

June 11, 2022  ‐ 1 min read

The artisan make:{...} commands provide you with some sweet scaffolding. You can quickly create a model, controller, test, command, etc. with a single command. What makes this even more convenient is that you can create classes related to the thing you make all at once.

Thus, creating a migration, controller and factory for a model you wish to generate can be done with just a single command. To do so we need to pass the options -mcf.

$ php artisan make:model Post -mcf

These short options stand for:

  • -m A migrations for the new model.
  • -c A controller for the new model.
  • -f A factory for the new model.

Instead of the short options you can use the long options instead for a bit more verbosity.

$ php artisan make:model Post --migration --controller --factory

All the options are detailed in the command help, which you can see by passing the --help flag.

$ php artisan make:model --help

This shows an --all option as well, which aggregates the creation of a migration, seeder, factory, policy and resource controller for the new model.

$ php artisan make:model Post --all