簡體   English   中英

如何使用Eloquent ORM訪問第二個表關系?

[英]How to access second table relationship using Eloquent ORM?

所以我有3個模型:

// Customer

public function invoices() {
    return $this->hasMany('App\Invoice');
}

// Invoice

public function payments() {
    return $this->hasMany('App\Payment');
}

// Payment

public function invoice() {
    return $this->belongsTo('App\Invoice');
}

在我的控制器中,我想訪問客戶的所有發票。

Customer::findOrFail($customer_id)->invoices;

上面的代碼運行良好,但我還想附加客戶每張發票的所有付款。

我嘗試做Customer::findOrFail($customer_id)->invoices->payments; 但看起來客戶模型也在尋找付款方式。

我在這里想念什么嗎?

$customer = Customer::with(array('invoices','invoices.payments'))->where('customer_id',$customer_id)->get();

采用:

$customer = Customer::with('invoices.payments')->findOrFail($customer_id);
$customer = Customer::find($id)->with('invoices.payments')->get();

雄辯的渴望加載文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM