Laravel 5.3 How to show Username in Notifications

2019-04-29 13:28发布

I am trying to add the user's first name in the notification emails. At the moment, Laravel notification emails starts like:

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?

3条回答
Summer. ? 凉城
2楼-- · 2019-04-29 14:08

You have to edit toMail function in App\Notifications\PasswordReset to set greeting as you want.

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.

protected $username = null;

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

When you initialize App\Notifications\PasswordReset, you can set the name.

In User model update the function as below.

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

    $this->notify($resetNotification);
}
查看更多
你好瞎i
3楼-- · 2019-04-29 14:11

The $notifiable variable passed to toMail() is User model.

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!');
}
查看更多
Emotional °昔
4楼-- · 2019-04-29 14:26

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!');
    }
}
查看更多
登录 后发表回答