[英]How to access second table relationship using Eloquent ORM?
So I have 3 models: 所以我有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');
}
And in my controller, I want to access all invoices by a customer. 在我的控制器中,我想访问客户的所有发票。
Customer::findOrFail($customer_id)->invoices;
The code above is working well, but I also wanted to attach all payments for each invoice of a customer. 上面的代码运行良好,但我还想附加客户每张发票的所有付款。
I tried doing Customer::findOrFail($customer_id)->invoices->payments;
我尝试做
Customer::findOrFail($customer_id)->invoices->payments;
but it looks like the Customer model looks for the payments method too. 但看起来客户模型也在寻找付款方式。
Am I missing something here? 我在这里想念什么吗?
$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.