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

时间:2016-11-20 12:04:39

标签: laravel notifications laravel-5.3 laravel-mail laravel-notification

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

Hello,

我想将其更改为:

Hello Donald,

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

用户模型:

public function sendPasswordResetNotification($token)
        {
            $this->notify(new PasswordReset($token));
        }

应用\通知\ 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 [
            //
        ];
    }
}

用户模型是否自动与通知类绑定?如何在视图中添加用户名?

3 个答案:

答案 0 :(得分:22)

传递给$notifiable的{​​{1}}变量是用户模型。

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

toMail()

答案 1 :(得分:8)

试试这个:

  

用户模型:

public function sendPasswordResetNotification($token) {
    return $this->notify(new PasswordReset($token, $this->username));
}
  

应用\通知\ 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!');
    }
}

答案 2 :(得分:2)

您必须在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!');
}

<强>更新

要设置$username,必须定义一个变量&amp; App\Notifications\PasswordReset中的setter方法。

protected $username = null;

public function setName($name) {
    $this->username = $name;
}

初始化App\Notifications\PasswordReset时,您可以设置名称。

User模型中更新功能如下。

public function sendPasswordResetNotification($token) {
    $resetNotification = new ResetPasswordNotification($token);
    $resetNotification->setName($this->name);

    $this->notify($resetNotification);
}