我正在从laravel 5.2迁移到5.3版本,希望在用户想要重置密码时发送自定义文本。
现在我发现,laravel使用通知(Notification),默认的“主题(theme)”是硬编码在laravel核心代码中的。
我已经有了一个视图(来自5.2),并且通知可以使用自定义视图,因此我尝试了以下内容:
在User.php(Model)中:
我创建了名为“SendLinkMailPasswordReset”的通知,用于覆盖Laravel的默认通知。以下是我的toMail()方法:
如果我执行
我不知道
在User.php(Model)中:
/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new SendLinkMailPasswordReset($token, $this->full_name));
}
我创建了名为“SendLinkMailPasswordReset”的通知,用于覆盖Laravel的默认通知。以下是我的toMail()方法:
我在这里重写了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');
} );
}
它起作用了。我的通知使用是否恰当,或者在我的情况下我应该发送电子邮件而不是推送通知?