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

8
我将尝试在通知邮件中添加用户的名字。目前,Laravel通知邮件的开头如下所示:
Hello,

我希望您能将其更改为:


Hello Donald,

现在,我的设置是这样的。以下示例是有关密码重置通知电子邮件的:

用户模型:

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 [
            //
        ];
    }
}

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

现在你的邮件发送没有用户名了吗? - Mohammad Nurul Huda Rimon
你好!在通知邮件中,默认会以“Hello!”开头。如果我在blade中添加$user->first_name$first_name,它会抛出未定义变量的错误。因此,我认为用户对象需要从通知类中发送? - Neel
你为此创建了一个邮件类和视图吗? - ABDEL-RHMAN
不需要。因为这是一个通知,我已经创建了名为“PasswordReset”的通知类。 - Neel
3个回答

40
$notifiable变量传递给toMail()方法的是User模型。
调用所需的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!');
}

9

试试这个:

用户模型:

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

那没起作用。我收到了错误信息:Argument 1 passed to App\Notifications\PasswordReset::__construct() must be an instance of App\Models\User, string given, called in /app/Models/User.php on line 65 and defined。这是因为在调用此通知类时传递了 $token。所以当我更改为:public function __construct($token, User $user),现在我得到了错误 Argument 2 passed to App\Notifications\PasswordReset::__construct() must be an instance of App\Models\User, none given and defined。它期望在调用该类时传递User模型。 - Neel
太完美了,正是我想要的。这一行代码 return $this->notify(new PasswordReset($token, $this->username)); 解决了我的问题。我不知道我可以直接从用户模型中的重写方法发送用户名。非常感谢! - Neel
@RimonKhan,你能解释一下为什么在密码重置工作流程中这个代码可以运行吗?我不明白在密码重置代码中是如何实例化或传递User实例的。 - Summer Developer
哦,抱歉,没事了。 $this->email 对我有用,因为我的 User 模型没有 username 属性。 :) - Summer Developer
2
$notifiable 已经传递到 toMail($notifiable)。这是您的用户模型。 - BenNov

2
您需要编辑 App\Notifications\PasswordReset 中的toMail函数,以您想要的方式设置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中定义一个变量和一个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);
}

但是这会导致错误 Undefined variable: username。通知类如何获取用户详细信息? - Neel
@Neel 你需要添加一个类变量并设置它。已更新答案,请检查。 - Saumini Navaratnam
根据文档(https://laravel.com/docs/5.3/passwords#password-customization),我创建了一个自定义通知类“PasswordReset”,并覆盖了用户模型上的“sendPasswordResetNotification”方法(如上所示)。默认情况下,“$token”会传递到通知类,但不包括用户数据。在通知类中,我考虑通过检查数据库表中的令牌来查找用户的名称,然后使用电子邮件地址找到该用户。但这似乎是一种冗长的方式,我想知道是否有更简单的方法来查找此用例的用户名。希望这有意义。 - Neel
@Neel,已经再次更新了答案。希望这可以解决您的问题。 - Saumini Navaratnam
@Neel,你可以在User模型中使用$this->name。根据你的代码更新类名即可。 - Saumini Navaratnam
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接