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

Nee*_*eel 5 notifications laravel laravel-mail laravel-5.3 laravel-notification

我想在通知电子邮件中添加用户的名字.目前,Laravel通知电子邮件开始如下:

Hello,
Run Code Online (Sandbox Code Playgroud)

我想将其更改为:

Hello Donald,
Run Code Online (Sandbox Code Playgroud)

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

用户模型:

public function sendPasswordResetNotification($token)
        {
            $this->notify(new PasswordReset($token));
        }
Run Code Online (Sandbox Code Playgroud)

应用程序\声明\ 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 [
            //
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

小智 25

$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!');
}
Run Code Online (Sandbox Code Playgroud)


Rim*_*han 8

试试这个:

用户模型:

public function sendPasswordResetNotification($token) {
    return $this->notify(new PasswordReset($token, $this->username));
}
Run Code Online (Sandbox Code Playgroud)

应用程序\声明\ 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!');
    }
}
Run Code Online (Sandbox Code Playgroud)

  • `$notifying` 已传递给 `toMail($notifying)`。这就是你的用户模型。 (2认同)