Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ThePenguin's avatar

Specify queue on queued notifications

How do I specify which queue I want a Laravel 5.3 notification to use?

My current attempt is like so:

class FriendRequestCreated extends Notification implements ShouldQueue { use Queueable;

public function toMail($notifiable)
{
 // Create mail notification
    $message = new MailMessage();

    // Mail content
    $message -> subject("You have a new friend request!")
             -> line("<p>You've received a friend request from:</p>")
             -> line("<p>" . $this -> friend -> first_name . " " . $this -> friend -> last_name . "<br />")
             -> line($this -> friend -> location() . "</p>")
             -> line("<p>Click <a href='" . url("account/home") . "'>here</a>, or go to your account to respond.</p>");

    // Return completed email
    $this -> queue = "email";
    return $message;
}

}

I've then launched a worker like so:

php artisan queue:listen --queue=email

But the job isn't handled.

0 likes
5 replies
ThePenguin's avatar

UPDATE: I needed to do it this way:

$user -> notify((new App\Notifications\FriendRequestCreated(13)) -> onQueue("email"));

BUT,

How can I specify a different queue for each of the "to" methods e.g. mail, array etc.

cmathis's avatar

@ThePenguin I know this an older post, but did you ever get this resolved? I'm also trying to name my queue for notifications and cannot seem to find anyone who has successfully done this.

burlresearch's avatar

I'm a little confused as to your class structure, but one way you might refactor would be to move the toMail() code into a handle method like SendFriendRequestEmail::handle after running

php artisan make:job SendFriendRequestEmail

then you can specify the queue on which you want to run the job when you dispatch it. IE the body of your toMail() method above simply becomes:

public function toMail($notifiable)
{
    $job = (new SendFriendRequestEmail($notifiable))->onQueue('email');
    dispatch($job);
}
cmathis's avatar

@burlresearch Disregard my question, I have figured out how to accomplish this. For anyone else who may come across this question looking to achieve the same thing, I'll share the simple step I was missing. In the constructor of my Notification class I entered the following code to name my notification queue:

$this->queue = 'queue-name';
5 likes
may-96's avatar

You can also define the queue for different channels in the Notification class using the viaQueues method. Check the Laravel documentation.

public function viaQueues()
{
    return [
        'mail' => 'mail-queue',
        'slack' => 'slack-queue',
    ];
}
2 likes

Please or to participate in this conversation.