mardi 23 avril 2019

How to access the current user within a Notification?

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 passing in the initial $subscribers collection 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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire