Laravel自定义邮件通知表格

5

我正在使用Laravel通知发送电子邮件,但我想知道如何在文本行之间在电子邮件视图中显示表格。

我正在使用Laravel的默认视图,但我不知道如何传递表格。

这是我的toMail方法:

public function toMail($notifiable)
    {
        $message = new MailMessage();
        $message->subject('Command confirmation n°'.$this->order->id)
            ->replyTo('noreply@example.com')
            ->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;
    }

在 email.blade.php 视图中(默认的 Laravel 视图)我有以下代码:
{{-- 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

如何将Markdown表格插入文本中,并将其放置在两行文字之间,就像操作按钮那样?
1个回答

6
如果$this->order是一个公共属性,它将在视图文件中可用。 视图数据 通常情况下,您希望向视图传递一些数据,以便在渲染电子邮件的HTML时使用。有两种方法可以使数据对您的视图可用。首先,在可邮寄类上定义的任何公共属性都将自动提供给视图。例如,您可以将数据传递到可邮寄类的构造函数中,并将该数据设置为类中定义的公共属性:
否则,使用with方法传递数据到视图。
如果您想在将其发送到模板之前自定义电子邮件数据的格式,可以通过with方法手动将数据传递到视图中。通常情况下,您仍然会通过可邮寄类的构造函数传递数据;但是,您应该将此数据设置为受保护或私有属性,以便不会自动将数据提供给模板。然后,在调用with方法时,传递要在模板中提供的数据数组。
接下来添加表格组件并循环订单项创建行:
@component('mail::table')
| id | name | price | qty | subtotal |
| -- |:----:| -----:| ---:| --------:|
@foreach($order->cart as $item)
  // create table rows
@endforeach
@endcomponent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接