Laravel 5.3 How to show username in email

I am trying to add a username in notifications. Currently, Laravel notification emails begin as follows:

Hello,

And I want to change it to:

Hello Donald,

Now I have such a setting. This example is for password reset notification:

User Model:

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

App \ Messages \ PasswordReset:

class PasswordReset extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification 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 bound to the notification class? How to add a username to a view?

+4
source share
3 answers

Try the following:

User Model:

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

App \ Messages \ 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!');
    }
}
+6
source

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

You need to edit the function toMailin App\Notifications\PasswordResetto set greetinghow 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, you must define the variable method and setter in App\Notifications\PasswordReset.

protected $username = null;

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

During initialization, App\Notifications\PasswordResetyou can set a name.

In the model, Userupdate the function as shown below.

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

    $this->notify($resetNotification);
}
+2
source

Source: https://habr.com/ru/post/1661340/


All Articles