Laravel Artisan Commands
Artisan is Laravel's command-line tool. It helps developers perform common tasks without writing code manually.
Think of Artisan as Laravel's personal assistant. Instead of creating files, clearing cache, running migrations, or generating controllers manually, you can simply run a command.
For example:
php artisan make:controller UserController
Laravel automatically creates the controller file for you.
What is Artisan?
Artisan is Laravel's built-in Command Line Interface (CLI).
It allows you to:
- Create controllers
- Create models
- Create migrations
- Run database migrations
- Seed databases
- Clear cache
- Generate authentication files
- Run queue workers
- Create custom commands
- Optimize applications
All Artisan commands start with:
php artisan
Viewing All Available Commands
To see all Artisan commands:
php artisan list
Output:
cache
config
db
event
make
migrate
queue
route
storage
test
view
This displays every command available in your Laravel project.
Getting Help for a Command
To view information about a specific command:
php artisan help migrate
Output includes:
- Purpose
- Options
- Arguments
- Examples
Application Commands
Check Laravel Version
php artisan --version
Output:
Laravel Framework 13.x
Useful for verifying the installed version.
Start Development Server
php artisan serve
Output:
Starting Laravel development server...http://127.0.0.1:8000
Now open:
http://127.0.0.1:8000
in your browser.
Custom port:
Make Commands (Code Generators)
Laravel can generate files automatically.
Create Controller
php artisan make:controller ProductController
Creates:
app/Http/Controllers/ProductController.php
Resource Controller
A Resource Controller in Laravel is a specialized controller class designed to handle CRUD (Create, Read, Update, Delete) operations for a specific resource, such as posts, users, or products.
php artisan make:controller ProductController --resource
Generates methods:
index()
create()
store()
show()
edit()
update()
destroy()
Create Model
php artisan make:model Product
Creates:
app/Models/Product.php
Model + Migration
php artisan make:model Product -m
Creates:
Model/Product.php
database/migrations/xxxx_create_products_table.php
Model + Controller + Migration
php artisan make:model Product -mcr
Creates:
Model/Product.php
database/migrations/xxxx_create_products_table.php
App/Http/Controllers/ProductController.php
all at once.
Create Migration
php artisan make:migration create_products_table
Creates:
database/migrations/xxxx_create_products_table.php
Create Seeder
php artisan make:seeder ProductSeeder
Creates:
database/seeders/ProductSeeder.php
Create Factory
php artisan make:factory ProductFactory
Creates:
database/factories/ProductFactory.php
Used for generating fake data.
Create Middleware
php artisan make:middleware CheckAge
Creates:
app/Http/Middleware/CheckAge.php
Create Request Validation Class
php artisan make:request StoreProductRequest
Creates:
app/Http/Requests/StoreProductRequest.php
Create Event
php artisan make:event ProductCreated
Creates:
app/Events/ProductCreated.php
Create Listener
php artisan make:listener SendProductNotification
Creates:
app/Listeners/SendProductNotification.php
Create Job
php artisan make:job ProcessOrder
Creates:
app/Jobs/ProcessOrder.php
Used with queues.
Create Mail Class
php artisan make:mail WelcomeMail
Creates:
app/Mail/WelcomeMail.php
Create Notification
php artisan make:notification OrderShipped
Creates:
app/Notifications/OrderShipped.php
Create Policy
php artisan make:policy ProductPolicy
Creates:
app/Policies/ProductPolicy.php
Create Command
php artisan make:command SendEmails
Creates:
app/Console/Commands/SendEmails.php
Custom Artisan commands can be built here.
Migration Commands
Migrations manage database tables.
Run Migrations
php artisan migrate
Creates all pending tables.
Migration Status
php artisan migrate:status
Shows:
Ran?Migration NameBatch
Rollback Last Migration
php artisan migrate:rollback
Reverses the latest migration batch.
Rollback Multiple Steps
php artisan migrate:rollback --step=3
Rolls back the last 3 batches.
Reset All Migrations
php artisan migrate:reset
Drops all migrated tables.
Refresh Migrations
php artisan migrate:refresh
Equivalent to:
rollback then migrate
Fresh Migration
php artisan migrate:fresh
Drops all tables and recreates them.
Fresh Migration with Seeders
php artisan migrate:fresh --seed
Very useful during development.
Seeder Commands
Run All Seeders
php artisan db:seed
Run Specific Seeder
php artisan db:seed --class=ProductSeeder
Database Commands
Open Database Shell
php artisan db
Connects to your configured database.
Inspect Database
php artisan db:show
Shows:
- Database name
- Tables
- Size
- Connections
View Table Details
php artisan db:table users
Displays column information.
Cache Commands
Cache improves performance.
Clear Application Cache
php artisan cache:clear
Clear Configuration Cache
php artisan config:clear
Rebuild Config Cache
php artisan config:cache
Recommended for production.
Clear Route Cache
php artisan route:clear
Create Route Cache
php artisan route:cache
Improves route loading speed.
Clear View Cache
php artisan view:clear
Clear Event Cache
php artisan event:clear
Optimization Commands
Optimize Application
php artisan optimize
Caches:
- Config
- Routes
- Events
for better performance.
Clear All Optimization Files
php artisan optimize:clear
Removes all cached files.
Route Commands
List Routes
php artisan route:list
Shows:
Method
URI
Name
Controller
Middleware
Example:
GET /GET productsPOST products
Queue Commands
Queues process background jobs.
Start Queue Worker
php artisan queue:work
Processes jobs continuously.
Listen for Jobs
php artisan queue:listen
Similar to queue:work but reloads framework every job.
Retry Failed Jobs
php artisan queue:retry all
View Failed Jobs
php artisan queue:failed
Delete Failed Jobs
php artisan queue:flush
Storage Commands
Create Storage Link
php artisan storage:link
Creates:
public/storage
linking to:
storage/app/public
Required for uploaded files.
Event Commands
Generate Event Cache
php artisan event:cache
Discover Events
php artisan event:list
Shows registered events and listeners.
Testing Commands
Run All Tests
php artisan test
Run Specific Test
php artisan test tests/Feature/ProductTest.php
Run Tests with Coverage
php artisan test --coverage
Maintenance Mode Commands
Enable Maintenance Mode
php artisan down
Users see:
503 Service Unavailable
Enable with Secret URL
php artisan down --secret="admin123"
Disable Maintenance Mode
php artisan up
Site becomes accessible again.
Most Frequently Used Artisan Commands
php artisan serve
php artisan make:model Product -mcr
php artisan migrate
php artisan migrate:fresh --seed
php artisan route:list
php artisan cache:clear
php artisan config:cache
php artisan optimize
php artisan optimize:clear
php artisan queue:work
php artisan storage:link
php artisan test
php artisan down
php artisan up
Summary
Artisan is Laravel's command-line assistant that automates repetitive tasks. The most important command groups are:
- make: Create files (models, controllers, migrations, middleware)
- migrate: Manage database tables
- db: Database tools
- cache/config/view: Clear and build caches
- route: Inspect routes
- queue: Process background jobs
- storage: Manage file links
- test: Run automated tests
- optimize: Improve application performance
- down/up: Maintenance mode management
Mastering Artisan commands can dramatically speed up Laravel development and reduce manual work.