我在使用基于多個條件的 order by 時遇到問題。填充資訊最多的用戶應該顯示在頂部,然后是填充資訊較少的用戶。
$users = User::where('status',1)
->withCount('reviews')
->with('reviews','about')
->orderByRaw("CASE WHEN is_native != '0' AND photo != '' THEN 0 ELSE 1 END")// how i can match the about us relationship value here? means if user have added about intro then it should come first and reviews count?
->paginate(10);
這是我關于用戶的關系
public function about()
{
return $this->hasOne('App\UserAbout', 'user_id')->select('about');
}
注意:我正在嘗試用 CASE 來做,如果有其他好的選擇,請指出。
謝謝
uj5u.com熱心網友回復:
這意味著您必須按 about 的計數排序,然后按評論計數排序,這將得到您想要的結果:
$users = User::where('status',1)
->withCount(['reviews','about'])
->with('reviews','about')
->orderByRaw('about_count desc,reviews_count desc')
->paginate(10);
現在有 'about' 的用戶將擁有about_count=1其他人about_count =0
uj5u.com熱心網友回復:
正如@OMR 建議的那樣,您可以這樣做。但是您不需要使用原始查詢
$users = User::where('status',1)
->withCount(['reviews','about'])
->with('reviews','about')
->orderBy('about_count','desc')
->orderBy('reviews_count','desc')
->paginate(10);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/365698.html
上一篇:如何排除具有多個值的id?
