Laravel - create model, controller and migration in single artisan command
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
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: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: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.namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class CustomMakeControllerCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:controller-with-migration {name}';
/**
* 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'
]);
}
}
2. Modify your .env file to enable the new command:
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
3. Run the new command:
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.