Laravel send same notification via different methods

257 views Asked by At

I'm trying to ->notify() different users of the same type via different notification methods. For example: I have TicketCompleted notification and its via() method contains: return ['mail', 'database'];. I also have both methods toArray() and toMail() implemented. So what I'm trying to do is the following:

Model Client that has role Accountant should be notified only via toArray() method;

Model Client that has role Contact should be notified only via toEmail() method;

How to achieve this?

1

There are 1 answers

0
Thomas On BEST ANSWER

You can return different values in via():

public function via($notifiable)
{
  if ($notifiable->role === 'Accountant') {
    return ['database'];
  } else if ($notifiable->role === 'Contact') {
    return ['mail'];
  }

  // default for all other clients
  return [];
}

If you use the same Notification for other models you also have to check the class of $notifiable.