Artisan Commands
Complete guide to Laravel's command-line interface
Introduction to Artisan
Artisan is the command-line interface included with Laravel. It provides helpful commands for building your application.
Tip: Run
php artisan list to view all available commands.
Viewing All Commands
# List all available Artisan commands
php artisan list
# Get help for a specific command
php artisan help migrate
# Check Laravel version
php artisan --version
Common Commands
Development Server
# Start development server
php artisan serve
# Custom host and port
php artisan serve --host=0.0.0.0 --port=8080
Application Key
# Generate application key
php artisan key:generate
# Show current key
php artisan key:show
Clear Cache
# Clear application cache
php artisan cache:clear
# Clear all cache types
php artisan optimize:clear
Queue Management
# Start queue worker
php artisan queue:work
# Process queue jobs once
php artisan queue:listen
Make Commands
Generate new files and classes using Artisan make commands:
| Command | Description | Example |
|---|---|---|
make:controller |
Create a new controller | php artisan make:controller UserController |
make:model |
Create a new Eloquent model | php artisan make:model User |
make:migration |
Create a new migration file | php artisan make:migration create_users_table |
make:seeder |
Create a new database seeder | php artisan make:seeder UserSeeder |
make:factory |
Create a new model factory | php artisan make:factory UserFactory |
make:middleware |
Create a new middleware | php artisan make:middleware CheckAge |
make:request |
Create a new form request | php artisan make:request StoreUserRequest |
make:command |
Create a new Artisan command | php artisan make:command SendEmails |
Useful Make Flags
# Create model with migration, factory, and seeder
php artisan make:model User -mfs
# Create resource controller
php artisan make:controller UserController --resource
# Create API controller
php artisan make:controller API/UserController --api
# Create invokable controller
php artisan make:controller SendEmailController --invokable
Database Commands
Migration Commands
# Run all pending migrations
php artisan migrate
# Rollback last migration
php artisan migrate:rollback
# Rollback all migrations
php artisan migrate:reset
# Rollback and re-run all migrations
php artisan migrate:refresh
# Drop all tables and re-run migrations
php artisan migrate:fresh
# Run migrations with seed
php artisan migrate:fresh --seed
# Check migration status
php artisan migrate:status
Database Seeding
# Run database seeders
php artisan db:seed
# Run specific seeder
php artisan db:seed --class=UserSeeder
# Refresh database and seed
php artisan migrate:fresh --seed
Database Operations
# Show database information
php artisan db:show
# Show table information
php artisan db:table users
# Drop all tables and migrations
php artisan db:wipe
Cache Commands
Clear Cache
# Clear application cache
php artisan cache:clear
# Clear config cache
php artisan config:clear
# Clear route cache
php artisan route:clear
# Clear view cache
php artisan view:clear
# Clear all cache
php artisan optimize:clear
Cache Optimization
# Cache config files
php artisan config:cache
# Cache routes
php artisan route:cache
# Cache views
php artisan view:cache
# Optimize for production
php artisan optimize
Custom Commands
Create your own Artisan commands for application-specific tasks.
Creating a Custom Command
# Generate new command
php artisan make:command SendNewsletters
Example Command Class
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SendNewsletters extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'email:send-newsletters {--queue}';
/**
* The console command description.
*/
protected $description = 'Send newsletters to all subscribers';
/**
* Execute the console command.
*/
public function handle()
{
$this->info('Sending newsletters...');
// Your logic here
$this->info('Newsletters sent successfully!');
return Command::SUCCESS;
}
}
Running Custom Commands
# Run custom command
php artisan email:send-newsletters
# With options
php artisan email:send-newsletters --queue
Task Scheduling
Schedule Artisan commands to run automatically using Laravel's task scheduler.
Setting Up Cron
# Add this to your crontab
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Scheduling Commands
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
// Run daily at midnight
$schedule->command('email:send-newsletters')->daily();
// Run hourly
$schedule->command('backup:run')->hourly();
// Run every minute
$schedule->command('queue:work')->everyMinute();
// Custom schedule
$schedule->command('reports:generate')->dailyAt('13:00');
}
View Scheduled Tasks
# List all scheduled tasks
php artisan schedule:list
# Test scheduler
php artisan schedule:test