Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel | Passing variable in Notify

I want to send a mail to Notify, it works but when I try to put the variables, It returns that they are undefined. I don't understand how to pass a variable to Notify, I tried to do ->withResult($result) but it didn't work out. Here is the controller:

    $result = Review::where('user_hash', '=', $data['lname_c'])->where('email', $data['email_c'])->orderBy('created_at', 'desc')->first();
    $result->notify(new SendReview());

And my SendReview.php notifications:

public function toMail($notifiable)
{

    return $result['invitation_id']; // test to check if variable is passed
    return (new MailMessage)
                ->line('Test.')
                ->action('Nani', url($url))
                ->line('Thank you');
}

There is user_hash and invitation_id in my Review table, I want to pass them to the notify. When I do return $result['invitation_id']; it works. Hope I am understandable, I checked for duplicate questions and couldn't find one.

like image 808
Guilhem V Avatar asked Dec 02 '17 11:12

Guilhem V


People also ask

What is notify () in laravel?

Laravel Notify is a package that lets you add custom notifications to your project. A diverse range of notification design is available.

How do I count notifications in laravel?

For all unread notification you should count like below instead of auth()->user()->unreadNotifications->count() . Your method will load all the notifications and then count will be performed in php when you can simply get the count with single query.


2 Answers

This is how they do it in docs.

$arr = [ 'foo' => "bar" ];
$result->notify(new SendReview($arr));

And in your SendReview.php

...

protected $arr;

public function __construct(array $arr) {
        $this->arr = $arr;
}

public function toMail($notifiable) {
        // Access your array in here
        dd($this->arr);
}
like image 102
Paul Santos Avatar answered Oct 07 '22 03:10

Paul Santos


You must use $notifiable variable in your Notification class.

It is an instance of the class to which the notification is being sent. So here your review object is passed as $notifiable variable.

You can try to log it as logger($notifiable) in toMail() method and check its properties.

like image 2
Amit Gupta Avatar answered Oct 07 '22 04:10

Amit Gupta