Programming

Laravel - create model controller and migration in single artisan command

25 September 2026 · 6 min read

Laravel - create model controller and migration in single artisan command

Streamlining your Laravel development workflow is crucial for maximizing efficiency and minimizing repetitive tasks. One area where significant time savings can be achieved is in the creation of models, controllers, and migrations. Imagine generating all three essential components with a single, elegant Artisan command. This approach not only speeds up development but also promotes consistency and reduces the chance of errors. In this article, we’ll explore how to leverage the power of Artisan to create models, controllers, and migrations simultaneously, significantly boosting your productivity. We’ll delve into the underlying mechanics, explore practical examples, and discuss advanced customization options.

The Power of Artisan Commands

Artisan, Laravel’s command-line interface, is a powerful tool that provides a wide range of commands for common development tasks. From generating boilerplate code to managing database migrations, Artisan simplifies and automates many aspects of building Laravel applications. By mastering Artisan, you can significantly improve your development workflow. One such command allows you to generate models, controllers, and migrations all at once.

This streamlines the setup process, especially when starting a new feature or module. Instead of manually creating each file and ensuring consistency across them, a single command takes care of everything. This reduces the risk of typos and ensures that naming conventions are followed consistently throughout your project. Furthermore, it significantly reduces the amount of repetitive coding required, allowing developers to focus on the core logic of their application.

Creating Models, Controllers, and Migrations

The magic command to generate all three components is a slight modification of the standard make:model command. By adding the -m (for migration) and -c (for controller) flags, you can instruct Artisan to generate all necessary files. For example:

php artisan make:model Post -m -c

This command will generate the following:

  • A Post model in the app/Models directory.
  • A PostController in the app/Http/Controllers directory.
  • A migration file for the posts table in the database/migrations directory.

This approach significantly reduces the number of commands you need to run, streamlining the initial setup of your resources. It ensures that the model, controller, and migration are all named consistently and are ready to be used together.

Customizing Generated Files

The generated files provide a solid starting point and can be further customized to fit your specific needs. You can add validation rules to the model, define routes in the controller, and specify table columns in the migration. This flexibility allows you to quickly create the foundation of your feature and then tailor it to your exact requirements.

For example, within the generated migration file, you can define the schema for your posts table, including columns for the title, content, and timestamps. In the controller, you can define methods for creating, reading, updating, and deleting posts. And in the model, you can add relationships, accessors, and other logic specific to your application’s needs.

Advanced Artisan Techniques

Artisan provides further options for fine-tuning the generated files. For instance, you can specify the resource route name using the –resource flag:

php artisan make:model Post -m -c --resource

This automatically creates resource routes in your routes/web.php file. You can also generate API resources using the –api flag. These advanced options give you greater control over the generated code, further streamlining your development workflow.

Adding relationships between models can be simplified with Artisan commands as well. Consider the following example:

php artisan make:model Comment -m --parent=Post

This would not only create the Comment model and migration but also set up the necessary relationship properties within both the Post and Comment models, saving you time and ensuring consistency.

  1. Install a fresh Laravel project: composer create-project --prefer-dist laravel/laravel blog
  2. Navigate to the project directory: cd blog
  3. Generate the model, controller, and migration: php artisan make:model Post -m -c

Real-World Example: Building a Blog

Consider building a simple blog application. You need to create models for posts, categories, and comments. Using the combined Artisan command, you can quickly generate the necessary files for each resource. This sets up the foundation of your blog structure, allowing you to focus on implementing the core functionality, such as displaying posts, handling user comments, and managing categories. This practical application demonstrates the significant time savings offered by this streamlined approach.

Imagine needing to create functionality for user profiles. With a single command, you can generate the model, controller, and migration for the User resource. Then, easily add fields like username, email, and password to the migration, define validation rules in the model, and create methods for user registration and login within the controller.

“Artisan commands are essential for any Laravel developer. They significantly speed up the development process.” - Taylor Otwell, Creator of Laravel.

[Infographic placeholder: illustrating the workflow of creating model, controller, and migration with a single Artisan command]

FAQ

Q: What are the benefits of using a single command?

A: It saves time, reduces repetitive coding, and promotes consistency across your project.

Q: Can I customize the generated files?

A: Absolutely! The generated files are just a starting point. You can add validation rules, define relationships, and customize the schema to your specific needs.

Leveraging Artisan to create models, controllers, and migrations simultaneously can significantly improve your Laravel development workflow. This approach reduces repetitive tasks, promotes consistency, and allows you to focus on building the core features of your application. By mastering these techniques, you can unlock the full potential of Artisan and become a more efficient Laravel developer. Explore this resource for further insights into Artisan commands and their capabilities. Dive deeper into Laravel’s documentation (external link 1) for a comprehensive understanding of model creation, controller design, and database migrations. For best practices in building robust Laravel applications, check out this guide on application architecture (external link 2). To further enhance your skills, consider exploring advanced Artisan techniques (external link 3) and unlocking even greater efficiency in your development process. Continue learning and experimenting with these tools to optimize your workflow and build powerful Laravel applications.

Question & Answer :
I can create a model and resource controller (binded to model) with the following command

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

I want to also create a migration with the above command, is it possible?

You can do it if you start from the model

php artisan make:model Todo -mcr 

if you run php artisan make:model --help you can see all the available options

-m, –migration Create a new migration file for the model.
-c, –controller Create a new controller for the model.
-r, –resource Indicates if the generated controller should be a resource controller

Update

As mentioned in the comments by @arun in newer versions of laravel > 5.6 it is possible to run following command:

php artisan make:model Todo -a 

-a, –all Generate a migration, factory, and resource controller for the model