I have 2 models Scheme & Sponsor with a many-to-many relationship with pivot SchemeSponsor.
The pivot table has a one-to-many relationship with ContributionRate. I want to get all sponsors related to a scheme together with all contributionrates.
Scheme
- id
- name
Sponsor
- id
- name
SchemeSponsor
- id
- sponsor_id
- scheme_id
- pivot_data
ContributionRate
- id
- scheme_sponsor_id
- rate
In the Sponsor model, I have this relationship
public function contribution_rates()
{
return $this->hasManyThrough(
ContributionRates::class,
SchemeSponsor::class,
'sponsor_id',
'scheme_sponsor_id',
'id',
'id'
);
}
This relationship returns all contributionrates even where the Scheme – Sponsor relationship does not exist. I want the rates that are related to the Pivot table SchemeSponsor. How can I achieve this via eager loading?
2
Answers
I have used EagerLoadPivotTrait by Arjon Jason Castro to eager load pivot relations.
Scheme Model
Sponsor Model
Then, to get the Scheme together with Sponsors and Sponsors' contribution rates;
I have noted in as much as $scheme is a single model, eagerloading sponsors with contribution rates returns all schemes with the sponsors plus the contribution rates.
Seems like you are wanting to do a has-many-through-many-to-many which isn’t supported by Laravel
You can, however, use eloquent-has-many-deep from jonas-staudenm (which makes your life much easier)
e.i. on Your Scheme Model