Just a code example from my project, showing the possibility of filtering hasMany relationships.
TagTypes -> hasMany Tags -> hasMany Examples
And you wanna query all the types, with their tags, but only those that have examples, ordering by most examples.
1$tag_types = TagType::with(['tags' => function ($query) {2 $query->has('examples')3 ->withCount('examples')4 ->orderBy('examples_count', 'desc');5 }])->get();