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";
});