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));
}
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();