Use Artisan::call() and Artisan::queue() to run console commands, clear caches, and trigger backups from HTTP controllers and background jobs.
When automating maintenance tasks, administrative dashboards, or deployment webhooks, you often need to run Artisan commands from within PHP code.
Laravel provides the Artisan facade to execute commands programmatically.
Running Synchronous Commands
use Illuminate\Support\Facades\Artisan;
// Run command and pass arguments/options
$exitCode = Artisan::call('cache:clear');
// Passing arguments and options as an array
$exitCode = Artisan::call('mail:send', [
'user' => 42,
'--queue' => 'default',
]);
// Retrieve command console output
$output = Artisan::output();
Running Asynchronous Commands in the Background with queue()
To prevent long-running commands from blocking HTTP web requests:
// Pushes command execution to the background queue worker
Artisan::queue('reports:generate-annual-pdf', [
'year' => 2026,
]);
Summary
- Executes registered Artisan commands from anywhere in the application.
Artisan::output()retrieves the string output of the executed command.Artisan::queue()delegates heavy CLI tasks to background queue workers.
Related Tips
View all tips →Process Improvements in Laravel 13.26: Idle Timeouts and Testing Assertions
Laravel 13.26 introduces ProcessIdleTimedOutException to distinguish idle processes from overall runtime timeouts, alongside new testing assertions for execution count and sequence order.
Create Lightweight CLI Tools with Closure Commands in routes/console.php
Use Artisan::command() in routes/console.php to build focused CLI commands using closures without creating dedicated Command classes.