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

Naysoewin's avatar

Send Notification to Multiple Roles

Hello,

1=> How to send notification to multiple roles when new Post created ? I put role name inside array but it doesn't send out to multiple roles , only send to one role that the first one of array

2=> According to docs, there is two ways to send notification via Trait and via Facade

May i know which one is better to send notification

Thanks all


public function created(Post $post)
    {
        $admin = User::whereHas('roles', function ($q) {
            return $q->whereName(['admin', 'author']);
        })->get();

        /** Using The Notifiable Trait */
        $admin->each(function ($admin) use ($post) {
            $admin->notify(new NewPostSubmitted($post));
        });

        /** Using The Notification Facade */
        Notification::send($admin, new NewPostSubmitted($post));
    }
0 likes
2 replies
bobbybouwmann's avatar
Level 88

Both work fine and should work in your case. The problem here is that you only select person that have both roles named admin and author. Try this query instead

$admins = User::whereHas('roles', function ($q) {
    return $q->whereIn('name', ['admin', 'author']); // If the user has one of the roles
})->get();
2 likes

Please or to participate in this conversation.