I have a model - let's call it Parent.
Each Parent can have many children.
public function children(): HasMany
{
return $this->hasMany(Child::class, 'parent_id', 'id');
}
I want to make a scope for Parent called "active".
My requirement goes as follows:
Parent is active when it has at least 1 child.
I've done local scopes multiple times and I know that within Parent.php I can do something like:
public function scopeActive($query)
{
return $query->where(…);
}
But as you see, $query is a \Illuminate\Database\Eloquent\Builder which is not my case. I want to operate on related model somehow, not the DB query.
Can I do this in a clean way without fetching all parents with('child') and within ->filter() forgetting those that have no children? When there will be 1000 parents but only one will have children, it will simply waste DB resources (by fetching 999 redundant parents).