intermediate 16 min read

Jobs & Queues

Creating jobs, dispatching, queue workers, job middleware, and Laravel 13 Queue::route().

Jobs

// Generate: php artisan make:job ProcessPodcast

namespace App\Jobs;

use App\Models\Podcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class ProcessPodcast implements ShouldQueue
{
use Queueable;

public function __construct(
public Podcast $podcast,
) {}

public function handle(): void
{
$this->podcast->process();
}
}

Dispatching Jobs

// Simple dispatch
ProcessPodcast::dispatch($podcast);

// Delayed
ProcessPodcast::dispatch($podcast)->delay(now()->addMinutes(10));

// Dispatch chain
Bus::chain([
new ProcessPodcast($podcast),
new OptimizePodcast($podcast),
])->dispatch();

// Batch
$batch = Bus::batch([
new ProcessPodcast($podcast1),
new ProcessPodcast($podcast2),
])->then(function () {
// All jobs completed
})->dispatch();

Laravel 13: Queue::route()

use Illuminate\Support\Facades\Queue;

// Route jobs to specific queues based on conditions
Queue::route(function (object $job) {
if ($job instanceof ProcessPayment) {
return "high";
}
if ($job->podcast->is_featured) {
return "featured";
}
return "default";
});

Examples

<?php
use App\Jobs\ProcessPodcast;
use App\Models\Podcast;

\$podcast = Podcast::factory()->create();
ProcessPodcast::dispatch(\$podcast)->onQueue('processing');
echo 'Job dispatched for podcast: ' . \$podcast->title;

Your Notes

Sign in to take notes for this lesson.

Quiz

Queues & Notifications Quiz

0 questions

Sign in to take quiz

Discussion

Sign in to join the discussion.

Flashcards

Sign in to create flashcards.