Laravel - create model, controller and migration in single artisan command
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
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=passwordCreating 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=TodoIncluding 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;
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'
]);
}
}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=passwordshell
php artisan custom-make-controller-with-migration TodoController