Generate Controller and Model

Generate Controller and Model

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Artisan Way: Generating Controllers and Models in Laravel

If you are stepping into the Laravel ecosystem, the first hurdle often involves understanding how to scaffold your application's structure without manually creating dozens of files. You might be looking at an old tutorial referencing Laravel 4 Beta or just starting a fresh project and wondering how to spin up a Controller and Model using the command line. The answer lies in php artisan, but the exact syntax and resulting file structures depend heavily on the version you are targeting. Since you mentioned experimenting with Laravel 4, it is crucial to recognize that the tooling has evolved significantly since then. While early versions of Laravel introduced these commands, modern iterations (Laravel 5 and beyond) offer more robust options for generating boilerplate code, though the core concept remains the same: use the CLI to automate repetitive file creation.

When you need to generate a Model, you typically interact with the make:model command. For a basic model named Post, the command is straightforward:

php artisan make:model Post -mcr

This single invocation creates three things: the Post model class itself, a corresponding migration file for the database table, and a seeder to populate initial data if you are using Laravel's seeding capabilities. The -m flag stands for migration, while -c adds a factory class (which is often paired with migrations in modern workflows) and -r creates a resource controller. If you are strictly on an older version like 4 where factories were less common, you might only need the model and migration flags.

However, generating a Controller requires careful attention to the type of controller you actually need. A standard "resource" controller is generated with:

php artisan make:controller PostController --resource

This command assumes that your Model will handle the database relationships (e.g., Post having many Comments). If you are creating a standalone controller, you might simply use make:controller PostController, which generates an empty class without any methods. This distinction is vital because mixing up the controller type and model flags can lead to mismatched expectations in your codebase. For instance, if you generate a resource controller but forget to link it to a model via the use statement inside the file, you will face errors when trying to access the $this->post property within your routes or controllers.

One common pitfall for new developers is assuming that these commands handle every detail automatically. In Laravel 4 and earlier, the generated code was often quite minimal, requiring you to manually add methods like index, create, and edit to your controller if you wanted them. Modern Laravel versions (5.x and 6+) are more opinionated; they generate full resource controllers with all standard CRUD methods pre-populated. This is a significant trade-off: you gain speed of development but lose some control over the default structure if you aren't careful to rename or remove unused methods immediately.

Another consideration is the namespace. In Laravel, models usually sit in the App\Models namespace (or App in older versions), while controllers often go into an Http\Controllers namespace. When using make:model, the command respects your APP_NAMESPACE configuration default, but if you have custom directory structures, you may need to specify the full path or adjust your composer.json autoload paths.

If you are maintaining legacy code from the Laravel 4 era, be aware that the php artisan interface has changed drastically. Commands that existed in Beta 4 might not exist in current stable releases, and vice versa. Always verify which version of the framework you are running by checking your composer.lock or running php artisan --version. If you find yourself stuck on an outdated setup, consider upgrading to a supported version where these commands are more reliable and better documented.

For further guidance on managing Laravel projects, including older versions or specific migration paths, resources like https://laravelcompany.com can provide context on ecosystem changes and best practices for maintaining legacy applications versus adopting modern standards.

Our network