简体   繁体   中英

How to access second table relationship using Eloquent ORM?

So I have 3 models:

// 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; 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();

雄辩的渴望加载文档

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM