lundi 29 mai 2017

How to sort users in laravel 5

In laravel 5.2, I want to get users by order country, then posts_count and then images_count. For that I have used below query.

User::with(['profiles' => function($query) use ($country){
                               $query->orderBy(DB::raw('case when country="'.$country.'" then 1 else 2 end'))->orderBy('country', 'ASC');
                        }])
                      ->withCount('posts')
                      ->withCount('images')
                      ->orderBy('posts_count', 'desc')
                      ->orderBy('images_count', 'desc')
                      ->get();

But this query sort users by posts_count, then images_count and then country. Please guide how can I fix the order priority country first in the query.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire