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!
`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`.
 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:
php artisan listLists 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).
php artisan help [command name]Example: php artisan help make:controller
Displays the parameters, options, and usage instructions of the specified command.
php artisan --version
# Or shorthand
php artisan -Vphp artisan key:generateGenerates 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.
php artisan config:cacheMerges 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.
php artisan config:clearDeletes the configuration cache file and restores dynamic loading of configuration files.
php artisan route:cacheCaches route definitions (suitable for production environments to improve route parsing speed). Only supports non-closure routes (routes must be defined in controllers).
php artisan route:clearmake Series Commands )Used to quickly generate core components such as controllers, models, and migration files, avoiding repetitive writing of basic code.
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.
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).
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.
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.
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.
make commands| Command | Description | 
|---|---|
| make:command | Generates a custom Artisan command | 
| make:component | Generates a Blade component | 
| make:factory | Generates a model factory (for batch creating test data) | 
| make:policy | Generates a policy class (for model authorization) | 
| make:provider | Generates a service provider | 
| make:seeder | Generates a database seeder (for initializing test data) | 
php artisan migrateExecutes all unrun migration files to create or modify database table structures.
Option: --force forces execution in the production environment (disabled by default).
php artisan migrate:rollbackRolls back the last migration operation (use --step=N to roll back the latest N migrations).
php artisan migrate:resetRolls back all executed migrations (from the latest to the earliest).
php artisan migrate:freshFirst deletes all tables (more thorough than reset), then re-executes all migrations. Suitable for resetting data in the development environment.
php artisan db:seedExecutes 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).
php artisan db:checkChecks if the database connection is normal (supported in Laravel 8+).
php artisan serveStarts 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).
php artisan route:listDisplays information about all registered routes (method, URI, controller, middleware, etc.).
Options: --name=keyword filters by route name; --path=URI filters by URI.
php artisan view:clearDeletes the compiled Blade view cache (in the storage/framework/views directory). Execute this if modifications to views do not take effect.
php artisan log:clearEmpties log files in the storage/logs directory.
php artisan tinkerStarts 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.
php artisan optimizeGenerates class map cache and route cache (equivalent to running config:cache, route:cache, and clear-compiled simultaneously) to improve performance in the production environment.
php artisan optimize:clearOne-click clears all caches (configuration, routes, views, compiled files, etc.) for both development and production environments.
# Enable maintenance mode (displays a 503 page)
php artisan down
# Disable maintenance mode
php artisan upOptions: down --message="Under maintenance..." customizes the prompt message; --retry=60 tells the client to retry after 60 seconds.
You can create your own Artisan commands using make:command:
Generate a command class: php artisan make:command SendEmails
Define the signature (command name and parameters) and handle method (execution logic) in app/Console/Commands/SendEmails.php.
Register the command: Add the command class to the $commands array in app/Console/Kernel.php.
Execute the custom command: php artisan send:emails
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