16

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.

4 Answers 4

27

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);
}
Sign up to request clarification or add additional context in comments.

10 Comments

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?
Why do you need to return the $notifiable['owner']? It does not make sense to me at all.
$notifiable['invitation_id']* sorry
You dont have to return those ids in toMail() method
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
|
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.

Comments

0

For people interested in passing an object e.g $msg_recipient and sending an email to a specific user from a controller.

Controller:

Remember to add use Illuminate\Support\Facades\Notification;

 Notification::route('mail',$msg_recipient->email)
                        ->notify(new MsgReceived($msg_recipient));

In your notification __construct

public function __construct($msg_recipient)
    {
        $this->username = $msg_recipient->username;
    }

toMail function

public function toMail($notifiable)
    {
        $username = $this->username;

        return (new MailMessage)
                    ->greeting('Hello, '.$username)
                    ->line('You received a brand new msg!')
                    ->action('View msg', url('/'.$username));
    }

Comments

0

The only way seems to be:

    public function toMail($notifiable)
{
    $message = (new MailMessage)
        ->greeting('Hi ' . $this->user->first_name . ',')
        ->subject($this->subject)
        ->line($this->body)
        // ->with(['id' => $this->user->id])
        ->line('')->view((str_contains($this->body, '</html>')) ? 'emails.empty' : 'emails.default');

    $message->viewData['id'] = $this->user->id;
    return $message;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.