Laravel在其中使用带有用户电子邮件参数的Notification Facade

时间:2018-11-23 02:48:40

标签: php laravel

我正在尝试发送带有名称或电子邮件参数的notification

控制器

$subscriber = Subscribe::whereNotNull('verified')->get();
Notification::send($subscriber, new NotifySubscriber($post));

toMail功能

public function toMail($notifiable)
{
    $url = url('/news/' . $this->post->slug);

    return (new MailMessage)
        ->greeting('Dear User')
        ->line('Title : ' . $this->post->title)
        ->action('Read More', $url)
}

我想要->greeting('Dear User')和用户名。像->greeting('Dear User' . $this->user->name)

以下内容也不起作用:

Notification::send($subscriber, new NotifySubscriber($post, $subscriber));

我该怎么办?

1 个答案:

答案 0 :(得分:0)

首先请确保在您的订阅模型中使用use Notifiable;

然后您可以访问$ notifiable,它是您发送到通知类的集合的一个实例。

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    $url = url('/invoice/'.$this->invoice->id);

    return (new MailMessage)
                ->greeting('Hello!'. $notifiable->user->name)
                ->line('One of your invoices has been paid!')
                ->action('View Invoice', $url)
                ->line('Thank you for using our application!');
}

查看this link更多详细信息。