Php 如何使用雄辩的ORM访问第二个表关系?
所以我有3个模型:Php 如何使用雄辩的ORM访问第二个表关系?,php,laravel,eloquent,Php,Laravel,Eloquent,所以我有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->
// 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;
我试着做Customer::findOrFail($Customer\u id)->发票->付款
但看起来客户模型也在寻找付款方法
我在这里遗漏了什么吗?使用:
$customer = Customer::with(array('invoices','invoices.payments'))->where('customer_id',$customer_id)->get();
使用:
这是可行的,所以我看到使用findOrFail方法只适用于模型自己的关系方法?这是可行的,所以我看到使用findOrFail方法只适用于模型自己的关系方法?
$customer = Customer::find($id)->with('invoices.payments')->get();