【发布时间】:2019-03-27 08:53:30
【问题描述】:
我正在从 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));
}
我创建通知“SendLinkMailPasswordReset”以“覆盖”laravel 和我的 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');
} );
}
它有效。我使用通知是好的还是我应该推送邮件?
【问题讨论】:
-
只有到下一个请求。它使用 Flash 会话
-
感谢您的回复。当我阅读文档laravel.com/api/5.3/Illuminate/Notifications/Messages/… 时,它说“在通知中添加一行文本”。这就是我使用它的原因。你知道另一种方法吗?
标签: php laravel laravel-5.3