Laravel | Passing variable in Notify

13,942

Solution 1

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);
}

Solution 2

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.

Share:
13,942
Guilhem V
Author by

Guilhem V

Updated on June 09, 2022

Comments

  • Guilhem V
    Guilhem V almost 2 years

    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.

  • Guilhem V
    Guilhem V over 6 years
    When I do dd($notifiable['invitation_id']) it return the value but I can't do return $notifiable['invitation_id'], how can I do it?
  • Paul Santos
    Paul Santos over 6 years
    Why do you need to return the $notifiable['owner']? It does not make sense to me at all.
  • Guilhem V
    Guilhem V over 6 years
    $notifiable['invitation_id']* sorry
  • Paul Santos
    Paul Santos over 6 years
    You dont have to return those ids in toMail() method
  • Guilhem V
    Guilhem V over 6 years
    I have to because I need to a button which redirected to /xxx/$notifiable['invitation_id']/
  • Paul Santos
    Paul Santos over 6 years
    Hold on. You're confusing me. Do you need to pass an array of data to your mail blade template?
  • Guilhem V
    Guilhem V over 6 years
    Let me re-explain all, so I have a table review with owner, invitation_id, user_hash etc... I did $result = Review::where('xx', '=', $xx etc..) and after $result->notify(new SendReview());, In my SendReview notification, I need to do the a button which redirected to the following link: **/xxx/{invitation_id from the $result column}/ and I need to return the $result->invitation_id to do url(/xxx/{invitation_id}/
  • Paul Santos
    Paul Santos over 6 years
    You can actually build the url in your toMail() method by accessing the notifiable parameter. And if ever you need to access the invitation_id after $result->notify(new SendReview()) you can just get it by using $result->invitation_id
  • Guilhem V
    Guilhem V over 6 years
    I got it! Thank you so much for helping
  • Paul Santos
    Paul Santos over 6 years
    Anytime brother.