Petyor
Petyor

Reputation: 404

Laravel send same notification via different methods

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?

Upvotes: 0

Views: 248

Answers (1)

Thomas
Thomas

Reputation: 8849

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.

Upvotes: 1

Related Questions