我正在从laravel 5.2迁移到5.3,当用户想要重置他的密码时,我想发送一个自定义文本。我现在明白了,laravel使用通知,默认的“主题”是硬编码在laravel内核中的。我已经有了一个视图(从5.2开始),通知可以使用自定义视图,所以我尝试了一下:
在User.php (模型)中
/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new SendLinkMailPasswordReset($token, $this->full_name));
}
我为“覆盖”laravel创建了我的通知"SendLinkMailPasswordReset“,这里是我的toMail()方法:
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->view('auth.emails.password')
->with
(
[
'user'=> $this->full_name,
'token'=> $this->token,
]
);
}
如果我执行dd($this->full_name)
,它是有效的,但当我重置我的密码时,我得到Undefined variable: user
我不知道with
是不是正确的方法,或者我是否想这样做是可能的。作为参考,如果我在我的sendPasswordResetNotification
中这样做
public function sendPasswordResetNotification($token)
{
$to=$this->email;
$user= $this;
Mail::send('auth.emails.password', ['user'=>$user, 'token'=>$token], function($message) use ($to) {
$message->to($to)->subject('Reset your password');
} );
}
它起作用了。我使用通知是好的,还是我应该推送邮件?
发布于 2017-01-25 17:08:06
尝尝这个
$user = $this->full_name;
$token = $this->token;
return (new MailMessage)
->view('auth.emails.password', compact('user','token'));
访问视图中的数据,如
{{ $user }}
{{ $token }}
发布于 2017-01-25 17:23:26
另一种方法是,我们可以像这样使用$viewData
属性(https://laravel.com/api/5.3/Illuminate/Notifications/Messages/MailMessage.html):
$viewData= [
'user'=>$this->full_name,
'token'=>$this->token
];
https://stackoverflow.com/questions/41847222
复制相似问题