Exciting news! TCMS official website is live! Offering full-stack software services including enterprise-level custom R&D, App and mini-program development, multi-system integration, AI, blockchain, and embedded development, empowering digital-intelligent transformation across industries. Visit dev.tekin.cn to discuss cooperation!

Complete Guide to Laravel Artisan Commands: From Basics to Advanced

2025-10-22 8 mins read

`Artisan` commands cover the entire Laravel development process. Proficiency in using them can greatly improve efficiency. In actual development, it is essential to master commonly used commands (such as `make:controller`, `migrate`, `serve`, and `tinker`). For other commands, you can always check the usage via `help`.

Artisan is the command-line tool of the Laravel framework, providing a wealth of practical commands to simplify the development process (such as code generation, database operations, server running, etc.). Below is a detailed explanation of commonly used artisan commands in Laravel, organized by function:

1. Basic Commands

1.1 List all commands

php artisan list
  • Lists all available artisan commands, including core commands and custom commands.

  • You can add -v (verbose) or filter by module (e.g., php artisan list make to view make related commands).

1.2 View command help

php artisan help [command name]
  • Example: php artisan help make:controller

  • Displays the parameters, options, and usage instructions of the specified command.

1.3 Check Laravel version

php artisan --version
# Or shorthand
php artisan -V

2. Project Initialization and Configuration

2.1 Generate application key

php artisan key:generate
  • Generates the APP_KEY in the .env file (used for encrypting sessions, cookies, etc.). Must be executed for newly installed projects.

  • Options: --show only displays the key without writing to the file; --force overwrites the existing key.

2.2 Cache configuration

php artisan config:cache
  • Merges all configuration files into a single cache file (bootstrap/cache/config.php) to improve loading speed.

  • Note: Re-run this command after modifying configurations; otherwise, the changes will not take effect.

2.3 Clear configuration cache

php artisan config:clear
  • Deletes the configuration cache file and restores dynamic loading of configuration files.

2.4 Cache routes

php artisan route:cache
  • Caches route definitions (suitable for production environments to improve route parsing speed). Only supports non-closure routes (routes must be defined in controllers).

2.5 Clear route cache

php artisan route:clear

3. Code Generation ( make Series Commands )

Used to quickly generate core components such as controllers, models, and migration files, avoiding repetitive writing of basic code.

3.1 Generate a controller

php artisan make:controller [ControllerName]
  • Example: php artisan make:controller UserController

  • Options:

    • --resource: Generates a resource controller (includes CRUD methods: index, create, store, etc.).

    • --model=ModelName: Associates a model (generates methods with model parameters).

    • --invokable: Generates a single-action controller containing only the __invoke method.

3.2 Generate a model

php artisan make:model [ModelName]
  • Example: php artisan make:model Post

  • Options:

    • --migration or -m: Generates a corresponding database migration file simultaneously.

    • --controller or -c: Generates a controller simultaneously.

    • --resource or -r: Generates a resource controller (used in conjunction with -c).

3.3 Generate a migration file

php artisan make:migration [MigrationFileName]
  • Migration files are used to define database table structures. The file name must follow conventions (e.g., create_users_table).

  • Example: php artisan make:migration add_email_verified_at_to_users_table

  • Options: --table=table_name specifies the table to modify; --create=table_name generates a migration for creating a new table.

3.4 Generate middleware

php artisan make:middleware [MiddlewareName]
  • Example: php artisan make:middleware CheckAge

  • The generated middleware is located in the app/Http/Middleware directory and needs to be manually registered in app/Http/Kernel.php.

3.5 Generate a request validation class

php artisan make:request [RequestClassName]
  • Example: php artisan make:request StoreUserRequest

  • Used to centrally manage form validation rules. Generated in the app/Http/Requests directory.

3.6 Other commonly used make commands

CommandDescription
make:commandGenerates a custom Artisan command
make:componentGenerates a Blade component
make:factoryGenerates a model factory (for batch creating test data)
make:policyGenerates a policy class (for model authorization)
make:providerGenerates a service provider
make:seederGenerates a database seeder (for initializing test data)

4. Database Operations

4.1 Run migrations (create/modify tables)

php artisan migrate
  • Executes all unrun migration files to create or modify database table structures.

  • Option: --force forces execution in the production environment (disabled by default).

4.2 Rollback migrations

php artisan migrate:rollback
  • Rolls back the last migration operation (use --step=N to roll back the latest N migrations).

4.3 Reset all migrations

php artisan migrate:reset
  • Rolls back all executed migrations (from the latest to the earliest).

4.4 Refresh the database (reset + re-run migrations)

php artisan migrate:fresh
  • First deletes all tables (more thorough than reset), then re-executes all migrations. Suitable for resetting data in the development environment.

4.5 Generate seed data

php artisan db:seed
  • Executes database seeders (classes in the database/seeders directory) to insert test data.

  • Option: --class=SeederName specifies a single seeder to execute (e.g., --class=UserSeeder).

4.6 Test database connection

php artisan db:check
  • Checks if the database connection is normal (supported in Laravel 8+).

5. Development Aids

5.1 Start the built-in server

php artisan serve
  • Starts the PHP built-in development server, default address: http://localhost:8000.

  • Options: --host=domain customizes the host; --port=port customizes the port (e.g., --port=8080).

5.2 View route list

php artisan route:list
  • Displays information about all registered routes (method, URI, controller, middleware, etc.).

  • Options: --name=keyword filters by route name; --path=URI filters by URI.

5.3 Clear view cache

php artisan view:clear
  • Deletes the compiled Blade view cache (in the storage/framework/views directory). Execute this if modifications to views do not take effect.

5.4 Clear log files

php artisan log:clear
  • Empties log files in the storage/logs directory.

5.5 Simulate HTTP requests

php artisan tinker
  • Starts an interactive PHP terminal where you can directly execute Laravel code (e.g., model queries, service calls) for easy debugging.

    • Examples: User::first() to view the first user; App\Models\Post::create(['title' => 'Test']) to create an article.

6. Production Environment Related

6.1 Optimize autoloading

php artisan optimize
  • Generates class map cache and route cache (equivalent to running config:cache, route:cache, and clear-compiled simultaneously) to improve performance in the production environment.

6.2 Clear all caches

php artisan optimize:clear
  • One-click clears all caches (configuration, routes, views, compiled files, etc.) for both development and production environments.

6.3 Maintenance mode

# Enable maintenance mode (displays a 503 page)
php artisan down

# Disable maintenance mode
php artisan up
  • Options: down --message="Under maintenance..." customizes the prompt message; --retry=60 tells the client to retry after 60 seconds.

7. Custom Commands

You can create your own Artisan commands using make:command:

  1. Generate a command class: php artisan make:command SendEmails

  2. Define the signature (command name and parameters) and handle method (execution logic) in app/Console/Commands/SendEmails.php.

  3. Register the command: Add the command class to the $commands array in app/Console/Kernel.php.

  4. Execute the custom command: php artisan send:emails

Summary

Artisan commands cover the entire Laravel development process. Proficiency in using them can greatly improve efficiency. In actual development, it is essential to master commonly used commands (such as make:controller, migrate, serve, and tinker). For other commands, you can always check the usage via help.

Image NewsLetter
Icon primary
Newsletter

Subscribe our newsletter

Please enter your email address below and click the subscribe button. By doing so, you agree to our Terms and Conditions.

Your experience on this site will be improved by allowing cookies Cookie Policy