【问题标题】:Laravel custom email notification tableLaravel 自定义邮件通知表
【发布时间】:2018-12-15 15:42:25
【问题描述】:

我正在使用 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 表如何放置,以及如何在行间放置,比如操作按钮?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

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

    View Data

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-12-09
      • 1970-01-01
      • 2017-08-01
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多