A Producer
hasOne
Distillery
, a Distillery
belongsToMany
Whiskies
.
A Whisky
belongsToMany
Distillery
, a Distillery
belongsTo
Producer
.
I wish to use hasManyThrough
so, to get a Producer
‘s Whiskies
that belongToMany
Distillery
.
But how can I use hasManyThrough
with belongsToMany
relationship? I am aware you can specify the join keys, and I think you may be able to muck about with the pivot table, but I can’t figure it out.
2
Answers
I think that you relationships shoul look like this:
To get the whiskies of a producer through a distillery, you can’t use hasManyThrough directly. Instead, you can define a custom method:
Unfortunately,
hasManyThrough
does not directly support your scenario.However, you can achieve your goal by manually defining a custom method in your
Producer
model to retrieve thewhiskies
throughdistilleries
.Also define the inverse relationship in your
Whisky
model to get theproducer
Now you can retrieve a producer’s whiskies like this: