Laravel 8 通過 with 方法

2021-07-19 10:46 更新

如果您想要在郵件數(shù)據(jù)發(fā)送到模板前自定義它們的格式,您可以使用 with 方法來手動傳遞數(shù)據(jù)到視圖中。一般情況下,您還是需要通過 mailable 類的構(gòu)造函數(shù)來傳遞數(shù)據(jù);不過,您應(yīng)該將它們定義為 protectedprivate 以防止它們被自動傳遞到視圖中。然后,在您調(diào)用 with 方法的時候,您可以以數(shù)組的形式傳遞您想要傳遞給模板的數(shù)據(jù):

<?php

namespace App\Mail;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class OrderShipped extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * 訂單實(shí)例。
     *
     * @var \App\Models\Order
     */
    protected $order;

    /**
     * 創(chuàng)建一個消息實(shí)例。
     *
     * @param  \App\Models\Order $order
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * 構(gòu)造消息。
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.orders.shipped')
                    ->with([
                        'orderName' => $this->order->name,
                        'orderPrice' => $this->order->price,
                    ]);
    }
} 
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號