Ad

Why Is Laravel Eager Loading Constraint Not Working?

- 1 answer

I have this as the basic of what i want to do but the constraint is not applying

$this['subs'] = Cat::with([
    'children' => function($query) {
        $query -> where('parent_id', 0);
    }
]) -> get();
Ad

Answer

the one you are looking for, is whereHas , like this:


$this['subs'] = Cat::whereHas('children', function ($query) {
                  $query->where('parent_id',0);
               })->get();

putting a query in with just scopes the relationships it queries, not the record set cat itself.

Ad
source: stackoverflow.com
Ad