Level 32
a link that may help you (http://stackoverflow.com/questions/34608205/how-to-order-by-count-of-related-items-in-eloquent-laravel-5-1)
just edit the query to fit your problem
I'm trying to sort users based on the followers count. I've a polymorphic relation to store the followers.
//To get the followers
$user->followers
and I'm trying to find the users with most followers and sort it with pagination. How do I go about it?
I used something like this once to order Photos by their number of views.
$photos = Photo::leftJoin('views','photos.id','=','views.photo_id')->
selectRaw('photos.*, count(views.photo_id) AS `count`')->
groupBy('photos.id')->
orderBy('count', 'DESC')->
get();
Maybe just replace Photo with User and the views with followers? This may work?
$users = User::leftJoin('followers','users.id','=','followers.user_id')->
selectRaw('users.*, count(followers.user_id) AS `count`')->
groupBy('users.id')->
orderBy('count', 'DESC')->
get();
Please or to participate in this conversation.