首页 > 解决方案 > Laravel 5.7 - 访问通知内视图模板

问题描述

$notifiable在我的视图模板中访问的正确方法是什么?

我知道$notifiable是用户,但是当我有以下

 public $abc;

 public function __construct($abc)
{
    $this->abc = $abc;
}


public function toMail($notifiable)
{

    $mailMessage = (new MailMessage)
        ->from('xyz@xyz.com', 'xyz company')
        ->subject('xyz')
        ->markdown('emails.news-alert');


    return $mailMessage;
}

在我的刀片模板中:

Hello {{ $notifiable->first_name }}

{{ $abc }}

上面会抛出一个错误,因为它无法识别$notifiable

但是,如果我按如下方式传递它,那么它可以工作:

$mailMessage = (new MailMessage)
        ->from('xyz@xyz.com', 'xyz company')
        ->subject('xyz')
        ->markdown('emails.news-alert', ['notifiable' => $notifiable);

不是$notifiable公共财产 - 我认为它可以作为默认视图提供给视图,而无需通过它?

标签: phplaravelnotificationslaravel-5.7

解决方案


是的,您需要发送要在视图中使用的变量

$mail->markdown(
   'emails.news-alert', [
      'notificable' => $notificable,
      'abc' => $this->abc
   ]
);

有时你可以使用 compact() 助手,但只有当你有命名变量时(不是 $this->)

$abc = $this->abc;
$mail->markdown('emails.news-alert', compact(['notificable','abc']));

请试试这个,让我知道它是如何工作的:)


推荐阅读