Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lebedev's avatar

Pass some variables to the markdown template within a notification

I am struggling with passing variables to a markdown template from the notification. My Notification Class:

use Queueable;

    public $data;
    
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
        //$this->onQueue('jobs_status');

        //$this->delay(Carbon::now()->addSeconds(35));
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {

        return (new MailMessage)->subject('Invoice Paid')->markdown('mail.new_job.user_notification', ['data' => $data]);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }

My markdown template:


@component('mail::message')
# Introduction

{{ $data }}

@component('mail::button', ['url' => $url])

@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent


This code throws an error : Undefined variable: data.

Do you know the correct way to pass variable to the markdown template?

Thank you in advance for any help!

0 likes
3 replies
RamjithAp's avatar

Try

  public function toMail($notifiable,$data)
    {
        return (new MailMessage)->subject('Invoice Paid')->markdown('mail.new_job.user_notification', ['data' => $data]);
    }
3 likes
sibongisenimsomis's avatar

Apparently passing variables to Markdown from Notification class works, differently.

I used compact on my solution, after struggle for about an hour cause I was referring to another example which was using with, on a Mailable class.

notification

return (new MailMessage)->markdown('mail.appointment.created', compact('appointment_reference', 'appointment_date'))
                                ->subject('Appointment Confirmation');

Mailable

return $this->markdown('mail.users.welcome')
                    ->subject('Welcome to ' .  config('app.name'))
                    ->with(['url' => route('password.reset', $this->getToken()),
                            'user' => $this->user]);
mvnrsa's avatar

i know this an old thread, but after struggling with this same issue for a while I want to help others save some time.

Passing the variables in the MailMessage::markdown method (as array or with compact) only passes them to the main view. It does not automatically pass them to components used by that view!

To use them in components like the header and footer you must also pass them to the mail message from the view (email.blade.php) like this:

@component('mail::message', ['notifiable'=>$notifiable])

and from message.blade.php to the individual components like this:

@component('mail::header', ['url' => config('app.url'), 'notifiable'=>$notifiable])

Please or to participate in this conversation.