Laravel 5.3如何在通知电子邮件中显示用户名 [英] Laravel 5.3 How to show Username in Notifications Email
本文介绍了Laravel 5.3如何在通知电子邮件中显示用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在通知电子邮件中添加用户的名字.目前,Laravel通知电子邮件的开头如下:
I am trying to add the user's first name in the notification emails. At the moment, Laravel notification emails starts like:
Hello,
我想将其更改为:
Hello Donald,
现在,我有这样的设置.此示例适用于密码重置通知电子邮件:
Right now, I have a set-up like this. This example is for a Password Reset Notification email:
用户模型:
public function sendPasswordResetNotification($token)
{
$this->notify(new PasswordReset($token));
}
应用\通知\密码重置:
class PasswordReset extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
用户模型是否自动与通知类绑定?如何在视图中添加用户名?
Is the User Model automatically binded with the Notification Class? How can I add the username in the view?
推荐答案
尝试一下:
用户模型:
User Model:
public function sendPasswordResetNotification($token) {
return $this->notify(new PasswordReset($token, $this->username));
}
App \ Notifications \ PasswordReset:
App\Notifications\PasswordReset:
class PasswordReset extends Notification
{
use Queueable;
public $username;
public function __construct($token, $username)
{
$this->username = $username;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello '.$this->username.',')
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}
}
这篇关于Laravel 5.3如何在通知电子邮件中显示用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文