After you get your collection of colors, you can sort them as a collection. See: http://laravel.com/docs/eloquent#collections ("Sorting a Collection by a Value")
Something like this:
$colors = Color::with('images')->get(); /* or whatever you're using to get your colors */
$colors = $colors->sortBy(function($color)
{
return $color->images->count();
});
Is it possible to do this in Eloquent instead of the collection? Not SQL dependent or anything.
I think this is impossible to do it efficiently without SQL. You need to use orderBy(DB::raw('COUNT(*)'))
in your code.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community