如何在laravel 5

时间:2017-05-29 07:02:06

标签: laravel laravel-5 laravel-5.2

在laravel 5.2中,我想按订单country,然后是posts_count然后images_count来吸引用户。为此,我使用了以下查询。

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();

但是此查询按posts_count排序用户,然后images_count然后country。 请指导我如何在查询中首先修复订单优先级country

1 个答案:

答案 0 :(得分:1)

试试这个

StreamObserver<Message> messageStream

不同的方式

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