You can still do it using the db.
Model::with([
'related' => fn ($query) => $query->where('xyz', 'abc')
])->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Assume I have the following Eloquent query (Since I will use the returned collection in more than one place, I eager load the related model) :
$myModels = MyModel::with(['myRelatedModel'])->where(...) ... ->get();
Now, what if I want to query $myModels to return only the ones where a field of myRelatedModel is xyz ? In a normal Eloquent query I'd use whereHas, but on collections that is not available.
$results = $myModels->where ??
Thanks
You can still do it using the db.
Model::with([
'related' => fn ($query) => $query->where('xyz', 'abc')
])->get();
Please or to participate in this conversation.