0
votes

On the Laravel docs, it states:

Using The Notification Facade

Alternatively, you may send notifications via the Notification facade. This is useful primarily when you need to send a notification to multiple notifiable entities such as a collection of users. To send notifications using the facade, pass all of the notifiable entities and the notification instance to the send method:

Notification::send($users, new InvoicePaid($invoice));

So I am doing this within my controller:

public function index()
{
  $subscribers = Subscribers::all();
  
  Notification::send($subscribers, new NewVacancy($subscribers));
}

And here is my Notification class

class NewVacancy extends Notification implements ShouldQueue
{
use Queueable;

public $subscriber; 

public function __construct( $subscribers)
{
    $this->subscriber = $subscribers;
}

public function toMail($notifiable)
{
 
  return (new MailMessage)->view(
    'mail.new-vacancy', 
    ['uuid' => $this->subscriber->uuid]// This fails as $subscriber is a collection
  );
}

....

The problem is that within the NewVacancy class, the $subscriber that is passed in is a full collection of all subscribers and not the individual notification being sent.

Now I know I could do a loop over $subscribers and fire the Notification::send() each time but that defeats the point of using facade to begin with.

The general goal is to send emails to all $subscribers with the ability to pass in unique subscriber data using a blade template.

2

2 Answers

1
votes

I found out you can access the current user via the $notifiable entity thats passed into the toMail() method.

public function toMail($notifiable)
{

  return (new MailMessage)->view(
    'mail.new-vacancy', 
    ['uuid' => $notifiable->uuid]
  );

}
0
votes

Please note that $notifiable represents the user object which is getting notified.

$user_id = $notifiable->id;