Reader Stacks

Laravel Artisan: Generating Controllers, Models, and More

Artisan's make: commands scaffold boilerplate consistently — the useful part is the flag shortcuts that generate a model together with its migration, factory, and controller in one command.

Hand-writing a new model, its migration, and a matching controller from scratch each time is both slower and more prone to inconsistency than using Artisan's generators — every make: command produces a correctly namespaced, conventionally structured class in the right directory automatically.

1. Generating a model

php artisan make:model Product

Creates app/Models/Product.php with the minimal boilerplate — extending Model, ready for $fillable, relationships, and casts to be added.

2. The flags that generate related files at the same time

php artisan make:model Product -m    # also creates a migration
php artisan make:model Product -f    # also creates a factory
php artisan make:model Product -s    # also creates a seeder
php artisan make:model Product -c    # also creates a controller
php artisan make:model Product -r    # controller is a resource controller (all 7 methods)
php artisan make:model Product --all # migration + factory + seeder + resource controller, all at once

--all (or the shorter -a, on some Laravel versions) is the fastest way to scaffold everything needed for a standard CRUD resource in one command — a migration, factory, seeder, and full resource controller, all correctly linked to the new model.

3. Generating a controller on its own

php artisan make:controller ProductController
php artisan make:controller ProductController --resource   # all 7 RESTful methods, stubbed out
php artisan make:controller ProductController --api        # resource methods, but no create/edit
                                                            # (no form views needed for a pure API)

4. Generating a migration on its own

php artisan make:migration create_products_table
php artisan make:migration add_price_to_products_table --table=products

As covered in an earlier migrations guide, Laravel infers a Schema::create vs. Schema::table stub from the migration's name — create_x_table versus anything else — which is why naming these consistently actually saves real boilerplate typing.

5. Other commonly used generators

php artisan make:middleware EnsureUserIsAdmin
php artisan make:request StoreProductRequest
php artisan make:event OrderShipped
php artisan make:listener SendShipmentNotification
php artisan make:job ProcessPayment
php artisan make:policy ProductPolicy --model=Product

Every one of these produces a correctly namespaced class extending the right base class, in the conventional directory — small individually, but the consistency compounds across a codebase with many contributors, since every generated file follows the exact same structure regardless of who ran the command.

6. Listing every available Artisan command

php artisan list
php artisan list make      # just the make: commands specifically

Worth running periodically — Artisan's command list is larger than what most day-to-day workflows touch, and includes generators and utilities (queue management, cache commands, custom app-specific commands) easy to forget exist.

Topics: Developer Productivity