Home > Software design >  Laravel Notification Markdown - Undefined variable
Laravel Notification Markdown - Undefined variable

Time:10-15

I am getting the following error when trying to pass data to a markdown mailable

Undefined variable: claim

I have the following code for a notification

class ChequeDiscrepancy extends Notification
{
    use Queueable;

    public $data;

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

    public function via($notifiable)
    {
        return ['mail'];
    }


    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Subject')
            ->markdown('emails.admin.banking.cheque_discrepancy');
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

In my markdown template, I have

@component('mail::message')
# Cheque No: {{$data->id}}
@endcomponent

According to https://laravel.com/docs/8.x/mail#view-data I should be able to pass the data via public properties or via The with Method. This works for regular mail but doesn't seem to work for notifications.

Am I doing something wrong?

Thanks

CodePudding user response:

Passing data is different between Notifications & Mailable classes. In notifications you need to pass the data explicitly, like this

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Subject')
            ->markdown('emails.admin.banking.cheque_discrepancy', ['data'=>$this->data]);
    }

You may read further here : Laravel Notifications

  • Related