首页 > 解决方案 > Laravel 自定义邮件通知表

问题描述

我正在使用 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

如何放置降价表,以及如何在行之间放置它,例如操作按钮?

标签: phplaravel

解决方案


如果$this->order是公共属性,它将在视图文件中可用。

查看数据

通常,您需要将一些数据传递给您的视图,以便在呈现电子邮件的 HTML 时使用这些数据。有两种方法可以使数据对您的视图可用。首先,在您的可邮寄类上定义的任何公共属性都将自动对视图可用。因此,例如,您可以将数据传递到可邮寄类的构造函数中,并将该数据设置为类上定义的公共属性:

否则使用该with方法将数据传递给视图。

如果您想在发送到模板之前自定义电子邮件数据的格式,您可以通过 with 方法手动将数据传递到视图。通常,您仍将通过可邮寄类的构造函数传递数据;但是,您应该将此数据设置为受保护或私有属性,这样数据就不会自动提供给模板。然后,在调用 with 方法时,传递一个您希望提供给模板的数据数组

接下来添加表格组件并循环创建行的订单项:

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

推荐阅读