gpt4 book ai didi

Laravel 5.3 如何在通知电子邮件中显示用户名

转载 作者:行者123 更新时间:2023-12-04 16:15:41 25 4
gpt4 key购买 nike

我正在尝试在通知电子邮件中添加用户的名字。目前,Laravel 通知邮件的开头如下:

Hello,

我想将其更改为:
Hello Donald,

现在,我有这样的设置。此示例适用于密码重置通知电子邮件:

用户型号:
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 [
//
];
}
}

用户模型是否自动与通知类绑定(bind)?如何在 View 中添加用户名?

最佳答案

$notifiable传递给 toMail() 的变量是用户模型。

调用所需的用户模型属性,简单:

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!');
}

关于Laravel 5.3 如何在通知电子邮件中显示用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40703804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com