Reputation: 97
I've only been able to pass dynamic data to my email notification body, not the header and footer, as it seems these are static templates which one can't pass variables directly to. Can this be done at all? I want to customise the header to fit each tenant who receives an email notification.
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$url = url('/invoice/'.$this->invoice->id);
return (new MailMessage)
->greeting('Hello!')
->line('One of your invoices has been paid!')
->action('View Invoice', $url)
->line('Thank you for using our application!');
}
How can I pass additional variables and access these in the header and footer of the notification template?
L
Upvotes: 2
Views: 2215
Reputation: 85
I have found another solution which is not documented but works like a charm.
step 1: as you know, have your blade published in 'email.blade.php'
step 2: inside this blade file refer to:
@component('mail::message', compact('add your field here'))
step 3: now, it is magically accessible in default message.blade.php
{{ $name of the field}}
step 4: you just have to provide it:
new MailMessage())->markdown('notifications::email', [ 'name of the field' => 'xxxxxxxxxxx',
Then you do not need to set in session.
Upvotes: 1
Reputation: 1461
Set them in the session and retrieve them later:
public function toMail($notifiable)
{
session()->now('arr', [
'url' => url('/invoice/'.$this->invoice->id)
]);
dispatch(function () {
session()->forget(['arr']);
})->afterResponse();
$url = url('/invoice/'.$this->invoice->id);
return (new MailMessage)
->greeting('Hello!')
->line('One of your invoices has been paid!')
->action('View Invoice', $url)
->line('Thank you for using our application!');
}
@component('mail::footer', ['arr' => session('arr') ?? []])
Now in the footer уоu can access the url as such $arr['url']
.
Upvotes: 0
Reputation: 744
I found a way to accomplish this using Markdown method. Basically you need to publish the email templates using the laravel command
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
then you need to access your Notification class and add the markdown method passing the default email template located in /resources/views/vendor/notifications/email.blade.php along with the data[] array for the with the dynamic content.
public function toMail($notifiable) {
return (new MailMessage)
->line('Line of text')
->markdown('vendor.notifications.email', [
'headerTitle' => 'Dynamic header title'
]);
}
now you have access to the $headerTitle variable inside your email.blade.php template file
@if(isset($test))
{{$headerTitle}}
@endif
Upvotes: 0