Samir B
Samir B

Reputation: 152

Laravel custom email notification table

I'm using laravel notifications to send emails, but I wonder how to show a table on email view between text lines.

I'm using the default view from laravel but I don't know how to pass the table'

Here is my toMail method:

public function toMail($notifiable)
    {
        $message = new MailMessage();
        $message->subject('Command confirmation n°'.$this->order->id)
            ->replyTo('[email protected]')
            ->line('Thanks to choose us!')
            ->line('Here the details of your order n°'.$this->order->id);

        foreach($this->order->cart as $item){
            // here the table
        }

        $message->line('To see your order click here');
        $message->action('My orders', url('http://www.example.com/espace-perso/mes-commandes'))

        return $message;
    }

In the email.blade.php view (default laravel view) I have:

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

{{-- Outro Lines --}}
@foreach ($outroLines as $line)
{{ $line }}

@endforeach

How to put the markdown table, and how to drop it between lines, like the action button?

Upvotes: 5

Views: 12343

Answers (1)

Brian Lee
Brian Lee

Reputation: 18187

If $this->order is a public property it will be available in the view file.

View Data

Typically, you will want to pass some data to your view that you can utilize when rendering the email's HTML. There are two ways you may make data available to your view. First, any public property defined on your mailable class will automatically be made available to the view. So, for example, you may pass data into your mailable class' constructor and set that data to public properties defined on the class:

Otherwise use the with method to pass data to the view.

If you would like to customize the format of your email's data before it is sent to the template, you may manually pass your data to the view via the withmethod. Typically, you will still pass data via the mailable class' constructor; however, you should set this data to protected or private properties so the data is not automatically made available to the template. Then, when calling the with method, pass an array of data that you wish to make available to the template

Next add the table component and loop the order items creating the rows:

@component('mail::table')
| id | name | price | qty | subtotal |
| -- |:----:| -----:| ---:| --------:|
@foreach($order->cart as $item)
  // create table rows
@endforeach
@endcomponent

Upvotes: 6

Related Questions