我有一个 message.blade,我试图在其中访问我的 Notification 类中设置的变量:
{{ $testVar }}
类SimpleMessage
有一个toArray
看起来像这样的方法:
public function toArray()
{
return [
'level' => $this->level,
'subject' => $this->subject,
'greeting' => $this->greeting,
'salutation' => $this->salutation,
'introLines' => $this->introLines,
'outroLines' => $this->outroLines,
'actionText' => $this->actionText,
'actionUrl' => $this->actionUrl,
'testVar' => 'test'
];
}
我正在MailMessage
用作 的子类SimpleMessage
,其中我有这个方法:
/**
* Get the data array for the mail message.
*
* @return array
*/
public function data()
{
return array_merge($this->toArray(), $this->viewData);
}
中没有toArray()
方法MailMessage
,所以指向超类SimpleMessage
。
但是,我收到此错误:
"message": "Undefined variable: testVar (View: ...\\message.blade.php)
如何正确将此变量传递给我的视图?
编辑 我的实现类如下所示:
class NewCommentNotification extends Notification
{
/**
* @var Examination
*/
private $examination;
public $testVar = 'bladiebla';
/**
* Create a new notification instance.
*
* @param Examination $examination
*/
public function __construct(Examination $examination)
{
$this->examination = $examination;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('message')
->subject('subject')
->action('action', 'url');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'id' => $this->examination->id,
'title' => 'title',
'uri' => 'edit',
'message' => 'message',
'action' => 'newExaminationCommentMessage',
'testVar' => $this->testVar
];
}
}