I have a query which isn't working as I want it to.
Product::with(['categories' => function ($categories) {
$categories->enabled();
}])
->with('media')
->get();
If we have a Product with no Categories which are enabled(), I don't want the Product returned. So basically, only give me Product where it has at least 1 enabled Category.
I read some old posts about whereHas but I can't find it in the Laravel Docs, has it been dropped?
You need whereHas to constraint by the relationship:
Product::whereHas('categories', function ($categories) {
$categories->enabled();
})->with(['categories' => function ($categories) {
$categories->enabled();
}])->with('media')->get();
Notice that the same constraint is being used for the eager-loaded categories as is used for the constraint; you can assign that Closure to a variable: