Laravel: get email recipient from within the mail

2019-07-17 15:29发布

When using Notifications in Laravel (5.5), you can access the Notifiable (the recipient) from within the toMail() method.

Is there an equivalent way to access the recipient when you are sending a Mailable?

Is there a better solution than just repeating the User instance in the constructor like this?

Mail::to($user)->send(new EventAlert($user));

1条回答
smile是对你的礼貌
2楼-- · 2019-07-17 15:45

No, there is no way. But you can use Mailable inside a Notification class and use the notification instead of Mailable in a controller or service.

From the docs:

You may return a mailable object from the toMail method

public function toMail($notifiable)
{
    return (new Mailable($this->invoice))->to($this->user->email);
}
查看更多
登录 后发表回答