2
votes

I am using Laravel Notifications to broadcasts updates to the user. I am wondering if there is a way to get the Notifiable target of the notification from inside the notification itself.

Auth::user()->notify(
new TextNotification('Hi there!')
)

Now, I want to get the Notifiable instance (here is the User) inside the Notification's __construct. Does this object get passed to the notification at any point?

I know I can pass it to the notification class, but I am wondering if this is already done by Laravel.

2

2 Answers

1
votes

yes, you get $notifiable instance in Notification class.

for more details see this.

1
votes

In this case the user is automatically inserted into the notification you're sending and you can accept it in the toArray and toMail methods.

// in TextNotification
public method toArray($notifiable)
{
    // $notifiable is the $user from Auth::user()
}