I have a message.blade in which I'm trying to access a variable set in my Notification class:
{{ $testVar }}
Class SimpleMessage
has a toArray
method that looks like this:
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'
];
}
I'm using MailMessage
as a child class of SimpleMessage
, in which I have this method:
/**
* Get the data array for the mail message.
*
* @return array
*/
public function data()
{
return array_merge($this->toArray(), $this->viewData);
}
There is no toArray()
method in MailMessage
, so that points to the superclass SimpleMessage
.
However, I get this error:
"message": "Undefined variable: testVar (View: ...\\message.blade.php)
How do I correctly pass this variable to my view?
EDIT My implementation class looks like this:
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
];
}
}