Multiple Call to Actions on Notifications

Published 9 months ago by Bartude

Hey guys,

According to the documentation: https://laravel.com/docs/5.5/notifications#markdown-mail-notifications I can add as many button components as I want to a notification, but it's not working. It's only adding one button and it's the last one in my notification:

public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->subject('Registration complete')
                    ->greeting('Hey ' . $this->name)
                    ->line('Here are two buttons!')
                    ->action('Buttons1', url('/button1url'))
                    ->action('Button2', url('/button2url'))
                    ->line('See you soon,');
    }

I've published the notification view and checked the MailMessage class and I can probably solve this by modifying a bit this class to make it work, but if in the documentation it says I can add multiple buttons, there must be something I'm missing. Any ideas?

Best Answer (As Selected By Bartude)
robrogers3

note:

you can see here:

@component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
@endcomponent

but there is only one $actionText, you need $anotherActionText, and url respectively.

Dry7
Dry7
9 months ago (151,880 XP)

@Bartude run

php artisan vendor:publish --tag=laravel-notifications

and add second button manually

Bartude

I'd already published that, but probably I didn't do it correctly:

{{-- Action Button --}}
@isset($actionText)
<?php
    switch ($level) {
        case 'success':
            $color = 'green';
            break;
        case 'error':
            $color = 'red';
            break;
        case 'normal':
            $color = 'blue';
            break;
        default:
            $color = 'purple';
    }
?>
@component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
@endcomponent

@component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
@endcomponent
@endisset

It just adds the same button twice

robrogers3

It doesn't look like you can call ->action twice.

But you can call line many times, and a Line can be an action.

->line(new Action('My Extra Action', url('foo')))

you can use ::with too!

no I'm wrong. it looks like there is only one call to action.

Look at SimpleMessage.php

you're going to have to override MailMessage and SimpleMessage.

Which wont' be hard.

Your CustomMailMessage needs to extend a RobustMessage.

And in robust message you'll need to allow two or more. it looks like adding an 'extra action' is the way to go -- easier.

See this in MailMessage.php

    /**
     * Get the data array for the mail message.
     *
     * @return array
     */
    public function data()
    {
        return array_merge($this->toArray(), $this->viewData);
    }

you'll need to have at least one more of these

            'actionText' => $this->actionText,
            'actionUrl' => $this->actionUrl,

and the same in your template.

robrogers3

note:

you can see here:

@component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
@endcomponent

but there is only one $actionText, you need $anotherActionText, and url respectively.

Bartude

@robrogers3 That solved my problem, but it's kind of limited. In the near future I'm going to see if I can make this more dynamic, probably with an array of buttons and doing a foreach inside the email template and submit a solution to git. Since this will be my first time doing a pull request(I think this is the correct name), does anybody have something I can read to help me with this in github?

robrogers3

there is an EXCELLENT example on laracasts.

https://laracasts.com/series/how-do-i/episodes/20

it does it from start to finish.

i did one but it was rejected. cause 5.5 already had it.

Please sign in or create an account to participate in this conversation.