Laravel 5.3 如何在通知邮件中显示用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40703804/
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
Laravel 5.3 How to show Username in Notifications Email
提问by Neel
I am trying to add the user's first name in the notification emails. At the moment, Laravel notification emails starts like:
我正在尝试在通知电子邮件中添加用户的名字。目前,Laravel 通知邮件开头如下:
Hello,
And I want to change it to:
我想把它改成:
Hello Donald,
Right now, I have a set-up like this. This example is for a Password Reset Notification email:
现在,我有这样的设置。此示例适用于密码重置通知电子邮件:
User Model:
用户模型:
public function sendPasswordResetNotification($token)
{
$this->notify(new PasswordReset($token));
}
App\Notifications\PasswordReset:
应用\通知\密码重置:
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?
用户模型是否自动与通知类绑定?如何在视图中添加用户名?
回答by Doctor Lines
The $notifiable
variable passed to toMail()
is User model.
$notifiable
传递给的变量toMail()
是用户模型。
Call to needed User model attribute, easy:
调用所需的用户模型属性,简单:
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello '. $notifiable->username)
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}
回答by Rimon Khan
Try this:
尝试这个:
User Model:
用户模型:
public function sendPasswordResetNotification($token) {
return $this->notify(new PasswordReset($token, $this->username));
}
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!');
}
}
回答by Saumini Navaratnam
You have to edit toMail
function in App\Notifications\PasswordReset
to set greeting
as you want.
您必须根据需要编辑toMail
功能以App\Notifications\PasswordReset
进行设置greeting
。
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!');
}
Update
更新
To set $username
, have to define a variable & setter method in App\Notifications\PasswordReset
.
要设置$username
,必须在App\Notifications\PasswordReset
.
protected $username = null;
public function setName($name) {
$this->username = $name;
}
When you initialize App\Notifications\PasswordReset
, you can set the name.
初始化时App\Notifications\PasswordReset
,可以设置名称。
In User
model update the function as below.
在User
模型中更新功能如下。
public function sendPasswordResetNotification($token) {
$resetNotification = new ResetPasswordNotification($token);
$resetNotification->setName($this->name);
$this->notify($resetNotification);
}