In this lesson, we'll learn about multiple queues and how to set job priorities. Sometimes, certain jobs are more important than others and need to be processed first, even if they were dispatched later.
Think of a real-life traffic scenario: cars are waiting at a traffic light, but an ambulance needs to pass through. Everyone moves aside to let the ambulance go first because it's a priority. We can implement similar priority rules with Laravel queues.
Dispatching Laravel Jobs To Different Queues
Let's look at a scenario where a user registers and triggers three different jobs. We want the last job to be processed before the other two:
use App\Jobs\CreateFirstTask;use App\Jobs\SendWelcomeMessage;use App\Jobs\SendRegisteredUserNotification; class RegisteredUserController extends Controller{ // ... public function store(Request $request): RedirectResponse { // ... CreateFirstTask::dispatch($user->id); SendWelcomeMessage::dispatch($user->id); SendRegisteredUserNotification::dispatch($user); Auth::login($user); return redirect(route('dashboard', absolute: false)); }}
To make the third job a priority, we can place it in a separate queue by using...