4

通知メールにユーザーの名前を追加しようとしています。私はセットアップこの等を有していて、今Laravel 5.3通知でユーザー名を表示する方法

Hello Donald, 

:現時点では、Laravelの通知メールは次のように起動します:

Hello, 

そして、私はそれを変更したいです。

ユーザーモデル::

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

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

が自動的に通知してバインドさユーザモデルであり、この例では、パスワードリセット通知メールのためでありますクラス?ビューにユーザー名を追加するにはどうすればいいですか?

+0

今、あなたの電子メールは、ユーザ名を指定せずに送信しますか? –

+0

はい。これはデフォルトである通知メールで 'Hello!'で始まります。 '$ user-> first_name'または' $ first_name'をブレードに追加すると、未定義の変数エラーがスローされます。だから私は、ユーザーオブジェクトは、通知クラスから送信する必要があると思いますか? – Neel

+0

これとビューのメールクラスを作成しましたか? –

答えて

6

これを試してみてください:

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

それはうまくいきませんでした。 'App \ Notifications \ PasswordReset :: __ construct()に渡される引数1は、App \ Models \ Userのインスタンスでなければなりません。指定された文字列は、65行目の/app/Models/User.phpで呼び出されます。定義された。これは、このNotificationクラスを呼び出すときに '$ token'が渡されるためです。だから、 'public function __construct($ token、User $ user)'に変更すると、 'App \ Notifications \ PasswordReset ::に渡された引数2を取得しています:: __ construct()はApp \ Models \ユーザ、与えられていない、定義されていない」。クラスを呼び出すときにユーザーモデルが渡されることを期待しています。 – Neel

+0

私は自分の答えを更新しました。今すぐチェックしてください。 –

+0

これは完璧です。まさに私が何をしたのか。この行は 'return $ this-> notify(新しいPasswordReset($ token、$ this-> username));'のためにそれを行いました。私は簡単にユーザーモデル内のオーバーライドメソッドからユーザー名を直接送ることができるかどうかはわかりませんでした。どうもありがとうございます! – Neel

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を設定するには、App\Notifications\PasswordResetで変数&セッターメソッドを定義する必要があります。

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); 
} 
+0

しかしそれは 'Undefined variable:username'というエラーを出します。通知クラスはどのようにユーザーの詳細を取得しますか? – Neel

+0

@Neelクラス変数を追加して設定する必要があります。応答が更新されました –

+0

私はユーザーモデルでどのように初期化するのですか? – Neel

4

toMail()に渡さ$notifiable変数は、Userモデルです。簡単に必要なユーザー・モデル属性に

コール、:

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

これは正解でなければなりません! – Hese

関連する問題