Use Artisan::queue() to push long-running Artisan commands to background queues instead of blocking HTTP requests.
Running heavy Artisan commands (such as generating bulk reports, clearing large cache stores, or sending system audits) inside HTTP controller requests blocks the user web server thread.
Artisan::queue() dispatches the command directly to background queue workers.
Code Example
use Illuminate\Support\Facades\Artisan;
// 1. Pushes command execution to default background queue
Artisan::queue('reports:generate', [
'--user' => $user->id,
]);
// 2. Specify dedicated connection and queue worker
Artisan::queue('reports:generate', ['--user' => $user->id])
->onConnection('redis')
->onQueue('exports');
Key Benefits
- Non-Blocking: Prevents HTTP timeouts by running commands asynchronously in background queue workers.
- Routing Control: Chain
onConnection()andonQueue()to route tasks to dedicated worker pools. - Argument Support: Accepts command arguments and flag options in standard key-value arrays.
Related Tips
View all tips →Monitor Aggregate Queue Metrics with Total Size Helpers in Laravel 13.27
Laravel 13.27 adds totalPendingSize(), totalDelayedSize(), and totalReservedSize() to the Queue facade, making it easy to aggregate queue metrics for dashboards and health checks.
Debounce Queued Event Listeners with #[DebounceFor] in Laravel 13.26
Laravel 13.26 extends the #[DebounceFor] attribute to queued event listeners, collapsing bursts of repeated events into a single execution based on the latest resource state.