Search code examples
laravellaravel-bladelaravel-10

Laravel Notifications pass variable from template to published vendor template


I've been building a mail template based on the published files you get from php artisan vendor:publish --tag=laravel-notifications

And I've successfully edited the message layout to include a powered-by message, however this piece does not use a variable.

Now I'm stuck trying to add a unique unsubscribe url to just above (see picture below) this powered-by but I'm unable to pass it "upwards" to the vendor template message.blade.php

This is part is from my toMail() function from the Notification which is ultimately called by a $user->notify(new Reminder(..)) function

return (new MailMessage())
    ->markdown('emails.reminder', ['unsubscribe_url' => 'custom_url_here'])

This is the editted message.blade.php file

<x-mail::layout>

{{-- Body --}}
{{ $slot }}

{{-- Subcopy --}}
@isset($subcopy)
<x-slot:subcopy>
    <x-mail::subcopy>
        {{ $subcopy }}
    </x-mail::subcopy>
</x-slot:subcopy>
@endisset

{{-- Footer --}}
<x-slot:footer>

    <x-mail::footer>
        <a href="{{ $unsubscribe_url }}">Unsubscribe here.</a>
    </x-mail::footer>

    <x-mail::powered-by />

    <x-mail::footer>
        © {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.')
    </x-mail::footer>
</x-slot:footer>
</x-mail::layout>

I've tried to use functions like extends, yield, include, ... inside the emails.reminder blade template

Also tried different ways of adressing the vendor template to no luck.

@extends('vendor.mail.html.message',[
    'unsubscribe_url' => $unsubscribe_url,
])

@yield('vendor.mail.html.message',[
    'unsubscribe_url' => $unsubscribe_url,
])

@include('vendor.mail.html.message',[
    'unsubscribe_url' => $unsubscribe_url,
])

@extends('x-mail::message',[
    'unsubscribe_url' => $unsubscribe_url,
])

@extends('x-slot:message',[
    'unsubscribe_url' => $unsubscribe_url,
])

@extends('x-mail::footer',[
    'unsubscribe_url' => $unsubscribe_url,
])

@extends('x-slot:footer',[
    'unsubscribe_url' => $unsubscribe_url,
])

@extends('mail::html.message',[
    'unsubscribe_url' => $unsubscribe_url,
])

@extends('mail::text.message',[
    'unsubscribe_url' => $unsubscribe_url,
])

The picture as mentioned above:

enter image description here


Solution

  • I was making it much more difficult than it had to be because I didn't quite understand mails and templates.

    The way you "pass variables to a higher laying template" in this case message.blade.php is just by defining an extra region where your text from the template you are using is pasted into.

    <!-- message.blade.php -->
    
    <x-mail::layout>
        @isset($foo) // if the section is optional
            <x-mail::foo>
                {{ $foo }}
            </x-mail::foo>
        @endisset
    </x-mail::layout>
    

    And define this slot in the template you are using by refering to slot:foo add passing which ever data you want to add there, in this case the unsubscribe url.

    <!-- reminder.blade.php -->
    
    <x-mail::message>
        <x-slot:foo>
            <a href="{{ $unsubscribe_url }}">Unsubscribe here.</a>
        </x-slot:foo>
    </x-mail::message>
    

    It's also required that if you add a new region you have to add this file foo.blade.php to the mail/html and mail/text folders