Laravel 8 通過 Public 屬性

2021-07-19 10:46 更新

通常情況下,您可能想要在渲染郵件的 HTML 內(nèi)容時傳遞一些數(shù)據(jù)到視圖中。有兩種方法傳遞數(shù)據(jù)到時視圖中。第一種,您在 mailable 類中定義的所有 public 的屬性都將自動傳遞到視圖中。因此,舉個例子,您可以將數(shù)據(jù)傳遞到您的 mailable 類的構(gòu)造函數(shù)中,并將其設(shè)置為類的 public 屬性:

<?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;

    /**
     * 訂單實例。
     *
     * @var Order
     */
    public $order;

    /**
     * 創(chuàng)建一個消息實例。
     *
     * @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');
    }
} 

當數(shù)據(jù)被設(shè)置成為 public 屬性之后,它將被自動傳遞到您的視圖中,因此您可以像您在 Blade 模板中那樣訪問它們:

<div>
    Price: {{ $order->price }}
</div> 
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號