Simpledev
Simpledev

Reputation: 647

Laravel notification email facade queue multiple users

Here is my code working to send notification email to multiple users

$users = User::whereIn('id', $userIds)->get();
\Notification::send($users, new DealPublished($deal));

It works but if I want to delay it like that

$users = User::whereIn('id', $userIds)->get();

$when = Carbon::now()->addSecond();

\Notification::send($users, new DealPublished($deal))->when($when);

Error is

FatalThrowableError in DealController.php line 226:
Call to a member function when() on null

How can I send notification email to multiple users using queue and Notification Facade ?

Thank's for help

Upvotes: 5

Views: 8881

Answers (3)

Stubbies
Stubbies

Reputation: 3124

Try it like this:

\Notification::send($users, (new DealPublished($deal))->delay($when));

Upvotes: 13

Simpledev
Simpledev

Reputation: 647

With a foreach loop

$when = Carbon::now()->addSecond();
 foreach($users as $user){
     $user->notify((new DealPublished($deal))->delay($when));
 }

It works, but if there is 1000+ users to notify, I'm not sure about the execution time :D

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

I think you should try this:

$when = Carbon::now()->addSecond(10);

 \Notification::send($users, new DealPublished($deal))->later($when);

OR

\Notification::send($users, new DealPublished($deal))->when($when);

Hope this work for you!

Upvotes: 2

Related Questions