Laravel - One notification for all users

Mody Sharf picture Mody Sharf · Oct 21, 2017 · Viewed 7.1k times · Source

I have social netwrok website using laravel. My notification system works well when the notification is set for a specific user. But suppose that a user became moderator and I want to notify all users of that event Is there a way to do so without inserting the same notification to all users?

One Solution that came to my mind is

I am thinking of setting the notification user_id to 0 which indicates that it is for all users, and by the way, I don't want the read_at property in this kind of notifications so no need for another table with a FK, So if this can be the solution so how to insert it and how to retrieve them along user's notifications relationship

Answer

vimuth picture vimuth · Apr 25, 2019

You can simply use Notification Facade. You can take all users and send them notification like this,

$users = Users::all();
Notification::send($users, new MyNotification($param));

And you need to call Notification facade with use like this,

use Illuminate\Support\Facades\Notification;