How to Use Artisan Commands in Laravel: Syntax and Examples
Use
php artisan followed by the command name to run Laravel's artisan commands in the terminal. These commands help you perform tasks like creating files, running migrations, and clearing caches quickly.Syntax
The basic syntax to run an artisan command is:
php artisan command:name [options] [arguments]
Here, command:name is the specific command you want to run, options modify the command behavior, and arguments provide extra data the command needs.
bash
php artisan migrate php artisan make:controller UserController php artisan cache:clear
Example
This example shows how to create a new controller using artisan and then clear the application cache.
bash
php artisan make:controller ProductController php artisan cache:clear
Output
Controller created successfully.
Application cache cleared!
Common Pitfalls
Common mistakes include:
- Running artisan commands outside the Laravel project directory.
- Forgetting to prefix commands with
phpif your system requires it. - Using incorrect command names or misspelling them.
- Not having the correct PHP version or dependencies installed.
Always check the available commands with php artisan list if unsure.
bash
Wrong: php artisan migrate Right: php artisan migrate
Quick Reference
| Command | Description |
|---|---|
| php artisan list | Shows all available artisan commands |
| php artisan help | Shows help for a specific command |
| php artisan make:model ModelName | Creates a new model |
| php artisan migrate | Runs database migrations |
| php artisan cache:clear | Clears the application cache |
Key Takeaways
Run artisan commands inside your Laravel project folder using 'php artisan'.
Use 'php artisan list' to see all available commands and their usage.
Always include 'php' before 'artisan' unless your system allows direct execution.
Artisan commands speed up common tasks like creating files and managing the app.
Check command spelling and options carefully to avoid errors.