staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

Using with a Belongs To Many? #204

Closed panthro100 closed 1 year ago

panthro100 commented 1 year ago

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.

Will this package help?

staudenmeir commented 1 year ago

Hi @panthro100, Use this relationship:

class Producer extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function whiskies()
    {
        return $this->hasManyDeep(Whisky::class, [Distillery::class, 'distillery_whisky']);
    }
}