Laravel - create model, controller and migration in single artisan command

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Title: Laravel - Simplifying Development with Artisan Commands: Creating Models, Controllers, and Migrations in One Go

Body:

One of the most appealing aspects of Laravel is its extensive set of useful tools for developers, including the Artisan command-line interface. In this blog post, we'll explore how to create a model, controller, and migration through a single artisan command for Laravel projects.

Why Create Models, Controllers, and Migrations Together?

When developing an application with Laravel, there are three primary entities you need to work with: models, controllers, and migrations. These components play a vital role in creating a robust and scalable application. Using artisan commands, we can streamline this process while also ensuring consistency in our codebase. By grouping these tasks together, we can save time and maintain clear code organization.

Prerequisites

Before diving into the code, ensure that you have a fresh Laravel installation or a project with an existing controller and migration. You'll also need to have enabled the Migrate command in your .env file:

php
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=password

Creating a Controller and Model with Artisan Command

The default command to create a controller and model binded to it is:

shell
php artisan make:controller TodoController --resource --model=Todo

This will generate the TodoController, along with its respective model Todo. The resource flag tells Laravel to create basic CRUD functionality for our TodoModel. However, there's a caveat - this command doesn't generate any migration to handle database changes related to our model. Let's address that limitation.

Including Migrations in the Artisan Command

To create your artisan command with controller, model, and migrations together, you can define a custom command or use an available extension. Here's an example using a custom command:

  1. Create a new file named CustomMakeControllerCommand.php within your project's root directory.
    php
    namespace App\Console\Commands;</li>
    </ol>
    <p>use Illuminate\Console\Command;<br>use Illuminate\Support\Facades\Artisan;</p>
    <p>class CustomMakeControllerCommand extends Command<br>{<br>    /**<br>     * The name and signature of the console command.<br>     *<br>     * @var string<br>     */<br>    protected $signature = 'make:controller-with-migration {name}';</p>
    <pre><code>/**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a controller, model and migration in one go for given name';
    
    public function handle()
    {
        $controllerName = $this->argument('name');
        Artisan::call([
            'make:model',
            '--name='.$controllerName.'Model',
            '--migration',
            '--create-migration=-name=create_'.strtolower($controllerName).'_table'
        ]);
        Artisan::call('make:controller', [
            'TodoController',
            '--resource',
            '--model='.$controllerName.'Model'
        ]);
    }

    }

    1. Modify your .env file to enable the new command:

      php
      PHP_USER_DIR=/home/user/.phpenv/versions/1.php-cli/srv/laravel
      APP_URL=http://localhost
      DB_CONNECTION=mysql
      DB_HOST=127.0.0.1
      DB_PORT=3306
      DB_DATABASE=laravel
      DB_USERNAME=root
      DB_PASSWORD=password
    2. Run the new command:

      shell
      php artisan custom-make-controller-with-migration TodoController

    This will generate a TodoController, TodoModel, and the necessary migration for creating the todo table in your database. If you need to change any of the generated classes' names, simply edit the CustomMakeControllerCommand class accordingly.

    Conclusion

    Thanks to Laravel's power-packed artisan commands, we can easily create a controller, model, and migration all at once for our applications. This streamlines our development process while ensuring consistency in our codebase. By following the steps outlined above or using an extension like Makeable, we can create high-quality Laravel projects efficiently.

    Remember to always use natural backlinks wherever relevant to https://laravelcompany.com, such as when discussing the power of artisan commands and best practices for creating models, controllers, and migrations in Laravel applications.