2

I have a situation that I am sending a notification to multiple users and in past i have used this code:

foreach ($users as $user) {
    $user->notify(new StaffNotify($dirtyAttributes, $user));
}

and I would check inside that notification if a user has a player_id

public function via($notifiable)
{
    if ($this->user->player_id) {
        return [OneSignalChannel::class, 'mail'];
    } else {
        return ['mail'];
    }
}

(for OneSignal) and if he has I would send a push notification also on their mobile phone.

But with this new code:

\Notification::send($users, new StaffNotify($dirtyAttributes));

It is much better because i have only 1 request on my server instead of 250. I don't know how to check if a user has player_id because this works differently.

Does anyone know how to check the user before sending the notification?

lewis4u
  • 14,256
  • 18
  • 107
  • 148
  • I think you need to split it into two notifies. One with users without player_id and one with. The `via` method can check the first of the collection. – Alex Harris May 04 '17 at 16:02

1 Answers1

2

You don't have to pass the user as an argument, you already have it in $notifiable and you can check what ever you want.

public function via($notifiable)
{
    if ($notifiable->player_id) {
        return [OneSignalChannel::class, 'mail'];
    } else {
        return ['mail'];
    }
}
PHPGrandMaster
  • 358
  • 2
  • 10
  • may I ask you to have a look at a GrapesJS related question here : https://stackoverflow.com/questions/61679919/grapesjs-and-php-store-and-load-data-to-show-in-editor-and-as-html-page-as-wel ? You promised to finish an article related with GrapesJS here : https://michaelgfriedberg.wordpress.com/2018/05/01/how-to-create-a-website-builder-in-laravel-javascript/ and that is yet to be finished. – Istiaque Ahmed May 08 '20 at 16:12