Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 1 year ago.
0

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();
});
Last updated 1 year ago.
0

Is it possible to do this in Eloquent instead of the collection? Not SQL dependent or anything.

0

I think this is impossible to do it efficiently without SQL. You need to use orderBy(DB::raw('COUNT(*)')) in your code.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

pidal pidal Joined 13 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.