Generate Controller and Model
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
A Comprehensive Guide on Generating Controllers and Models with Laravel's Command Line Interface
Laravel is a popular PHP framework that provides developers with several convenient tools to facilitate their tasks. Among these features, the command line interface (CLI) stands out as an essential tool for managing applications. One of its most useful functions is generating controllers and models, which can save time and reduce errors during development. In Laravel 4 Beta version, you usedphp artisan. Now in Laravel 5+ versions, the artisan command line interface has been improved with more functionalities. In this comprehensive guide, we will discuss how to generate controllers and models using the upgraded command-line tool.
Generating Controllers
A controller is a PHP class that handles user requests by coordinating between the requesting user and the model or database layer. You can create a new controller for handling specific tasks in your application using themake:controller command. Here's how to write it:
1. Start with the base command:
`php artisan make:controller ControllerName`
2. If you want to generate a controller within a specific namespace, use --namespace:
`php artisan make:controller ControllerName --namespace=App\Http\Controllers`
3. For creating a controller with the given name but in a different location, provide the full path:
`php artisan make:controller ControllerName --path=app/MyCustomControllerFolder`
4. To generate a controller with a custom class name (not the default), specify it using --class:
`php artisan make:controller CustomClass --class=MySpecialController`
5. Finally, to skip the scaffolded view and test files, use the --no-views flag:
`php artisan make:controller ControllerName --no-views`
Generating Models
A model represents a database table in your application. It handles the interaction with the database and provides methods to work with data. You can create models using themake:model command. Here's how to write it:
1. Start with the base command:
`php artisan make:model ModelName`
2. If you want to generate a model within a specific namespace, use --namespace:
`php artisan make:model ModelName --namespace=App\Models`
3. For creating a model with the given name but in a different location, give the full path:
`php artisan make:model ModelName --path=app/MyCustomModelFolder`
4. To generate a model with a custom class name (not the default), specify it using --class:
`php artisan make:model CustomClass --class=MySpecialModel`
5. Lastly, to skip the scaffolded view and test files for models, use the --no-views flag:
`php artisan make:model ModelName --no-views`
In conclusion, generating controllers and models in Laravel is a simple task once you understand the command line interface. Remember to practice using the available options as per your requirements. The Laravel documentation has additional information and examples on how to create more complex controllers and models.
For further insights into Laravel's capabilities and best practices, visit https://laravelcompany.com/blog/category/framework/, which features a series of articles explaining various aspects of the framework.