Laravel Artisan commands allow you to create custom CLI commands to automate repetitive tasks in your application. Here's a comprehensive guide on how to create one.
Use the Artisan make command to generate a new command class:
php artisan make:command SendEmailReport
This creates a new file at app/Console/Commands/SendEmailReport.php
Open the generated file and configure the command properties:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SendEmailReport extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'email:send-report {user} {--queue}';
/**
* The console command description.
*/
protected $description = 'Send email report to a specific user';
/**
* Execute the console command.
*/
public function handle()
{
// Command logic goes here
}
}
The signature defines how to call your command:
// Basic command
protected $signature = 'email:send-report';
// With required argument
protected $signature = 'email:send-report {user}';
// With optional argument
protected $signature = 'email:send-report {user?}';
// With option
protected $signature = 'email:send-report {--queue}';
// With option that accepts value
protected $signature = 'email:send-report {--type=daily}';
Add your command logic in the handle() method:
public function handle()
{
// Get arguments and options
$user = $this->argument('user');
$queue = $this->option('queue');
// Display output
$this->info('Sending email report...');
// Your business logic here
// Example: Send email, process data, etc.
// Progress bar example
$users = User::all();
$bar = $this->output->createProgressBar(count($users));
foreach ($users as $user) {
// Process user
$bar->advance();
}
$bar->finish();
// Success message
$this->info("\nEmail report sent successfully!");
return Command::SUCCESS;
}
Laravel provides helpful methods for interacting with the user:
public function handle()
{
// Ask for input
$name = $this->ask('What is your name?');
// Secret input (password)
$password = $this->secret('Enter password');
// Confirmation
if ($this->confirm('Do you want to continue?')) {
// Continue
}
// Choice selection
$role = $this->choice('Select role', ['admin', 'user', 'guest']);
// Display messages
$this->info('Information message');
$this->error('Error message');
$this->warn('Warning message');
$this->line('Regular message');
// Table output
$this->table(
['Name', 'Email'],
[
['John', '[email protected]'],
['Jane', '[email protected]']
]
);
}
Execute your custom command:
# Basic execution
php artisan email:send-report
# With arguments
php artisan email:send-report [email protected]
# With options
php artisan email:send-report --queue
# With both
php artisan email:send-report [email protected] --queue
To run the command automatically, add it to app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('email:send-report')
->daily()
->at('09:00');
}
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\User;
use App\Mail\DailyReport;
use Illuminate\Support\Facades\Mail;
class SendEmailReport extends Command
{
protected $signature = 'email:send-report {user_id} {--type=daily}';
protected $description = 'Send email report to a specific user';
public function handle()
{
$userId = $this->argument('user_id');
$type = $this->option('type');
$user = User::find($userId);
if (!$user) {
$this->error('User not found!');
return Command::FAILURE;
}
$this->info("Sending {$type} report to {$user->email}...");
Mail::to($user->email)->send(new DailyReport($user));
$this->info('Report sent successfully!');
return Command::SUCCESS;
}
}
This creates a fully functional custom Artisan command that you can use and modify for your specific needs!