Is it possible when you do a query on a model, to sort the result by the number of it's has_many's related items ? For example, order blog posts by number of comments...
No. That would require a select with a subquery, and that is not supported.
As a workaround you could: - run your query including the related has-many - loop over the result, and store the count of the related records in a custom variable - sort the array on that variable using Arr::sort() - send the sorted array to the view