Home > other >  Send notification to all users in laravel with all users data
Send notification to all users in laravel with all users data

Time:10-10

I want to send notification to all users and want to pass all users data to MyNotification class

Notification::send($users, new MyNotification($users));

In that way MyNotification class constructor will receive the collection of all users and it returns the error

Property [first_name] does not exist on this collection instance

MyNotificatioin.php

public function toMail($notifiable)
{
    return (new MailMessage)
        ->line('Dear ' . $this->users->first_name . ',')
        ->line('Your profile has been updated.')
        ->action('View Profile', url('/'));
}

So my question is, how I can pass all users data to MyNotification class or I will need to run foreach loop in MyNotification class to get every user's first_name ?

CodePudding user response:

In your toMail function you would use the $notifiable variable (which is the user) instead of trying to use the users collection.

public function toMail($notifiable)
{
    return (new MailMessage)
        ->line('Dear ' . $notifiable->first_name . ',')
        ->line('Your profile has been updated.')
        ->action('View Profile', url('/'));
}

I'm also not sure why you are passing all users in to the MyNotification, this is not required.

  • Related