staudenmeir / eloquent-has-many-deep

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

Ability to filter / add where clause to hasManyDeep relationship #94

Closed mattpramschufer closed 4 years ago

mattpramschufer commented 4 years ago

Currently I have an Organization model with the following relationship

public function cases() { return $this->hasManyDeep('App\Models\CourtCase', ['App\Models\Property', 'App\Models\Lease']); }

The model App\Models\CourtCase has a field called archive which is boolean. I wanted to create a new relationship like

public function archivedCases() { return $this->hasManyDeep('App\Models\CourtCase', ['App\Models\Property', 'App\Models\Lease'])->where('archive', '=', 1); }

But that doesn't actually work. Is there another method I can try or am I doing something incorrectly?

Thanks in advance.

mattpramschufer commented 4 years ago

Nevermind, I figured it out. I had a global scope present, I needed to then do

public function archivedCases() { return $this->cases()->withoutGlobalScopes()->where('archive', '=', 1)->get(); }