Laravel: Create an API Controller, model and migration in one line

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Streamlining Laravel Development with One-Line API Creation Creating an API often requires handling multiple tasks such as developing controllers, models, and migrations, which can be time-consuming and tedious. In this comprehensive blog post, we will explore how to optimize your workflow using a single line of code for these essential Laravel components. By understanding the benefits and best practices involved in this approach, you can significantly improve your productivity as a developer. 1. API Controller Creation: The first step is to create an API controller through the command line. Here's how it works:
php artisan make:controller API/name_of_controller --api --model=name_of_model
This will generate three files: - "Api/name_of_controller.php" with relevant controller code and routes, ready for use within your Laravel application. - "Model/name_of_model.php", a model class file that contains the table structure and related methods for database interaction. This model is already connected to your newly created API controller. - A "Controller.php" file can be found under the "API" folder, which extends the base Controller class from Laravel framework and includes a reference to your new controller. By utilizing this approach, you have combined three separate tasks into one efficient operation, simplifying the process of creating an API in Laravel. 2. Migration Creation: To continue with our streamlined API development methodology, we can then create a migration for your newly created model using this command:
php artisan make:migration create_users_table
This will generate the necessary migration file ("2018_07_04_151638_create_users_table.php") for your database table. You can then proceed to modify this file according to your desired data structure. In the past, you might have used the "-mcr" option in Laravel's make:model command to create both model and migration files simultaneously. However, using separate commands allows for greater flexibility and control over each module. It also makes it easier to work with different types of models and their respective migrations independently. 3. Enhancing Your Workflow: Now that we have explored the steps involved in creating an API controller, model, and migration one line at a time, consider the following tips to further improve your Laravel development process: - Embrace test-driven development (TDD) to ensure quality control throughout your project. Implementing unit tests and integration tests for both models and controllers is crucial in maintaining high code quality. - Use version control systems (VCS) such as Git, SVN, or Mercurial to keep track of all code changes and collaborate efficiently with team members. This practice enables you to track progress, store your work securely, and roll back to previous versions if needed. - Implement code coverage tools for better insight into the overall health of your Laravel application. Tools like PHPUnit and Codeception can help identify areas of improvement in your codebase. Conclusion: By leveraging Laravel's powerful command line interface (CLI) features, you can significantly streamline the process of creating an API controller, model, and migration all at once. This approach allows for enhanced productivity and a more efficient workflow. Remember that constant practice and dedication to best practices are crucial to becoming an expert Laravel developer. So go ahead and start optimizing your code with these helpful tips!