laravel 拉拉维尔 | 在通知中传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47607433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:02:09  来源:igfitidea点击:

Laravel | Passing variable in Notify

phplaravelemailnotify

提问by Laynom Graphic

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:

我想向 Notify 发送邮件,它可以工作,但是当我尝试放置变量时,它返回它们未定义。我不明白如何将变量传递给 Notify,我尝试这样做,->withResult($result)但没有成功。这是控制器:

    $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:

还有我的 SendReview.php 通知:

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.

我的Review表中有user_hash和invitation_id,我想将它们传递给notify。当我这样做return $result['invitation_id'];时。希望我可以理解,我检查了重复的问题,但找不到。

回答by Paul Santos

This is how they do it in docs.

这就是他们在文档中的做法。

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

And in your SendReview.php

而在你 SendReview.php

...

protected $arr;

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

public function toMail($notifiable) {
        // Access your array in here
        dd($this->arr);
}

回答by Amit Gupta

You must use $notifiablevariable in your Notificationclass.

您必须$notifiableNotification类中使用变量。

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

它是向其发送通知的类的实例。所以在这里你的评论对象作为$notifiable变量传递。

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

您可以尝试将其记录作为logger($notifiable)toMail()方法和检查其属性。