2017-05-26 121 views
0

我想基於特定值對用戶進行排序。例如,我有Users table並與Profiles Table有關係。在Profiles table我已保存Country (string)的用戶。根據Laravel中的特定值(字符串)對用戶進行排序

現在我想先排序特定的國家用戶,然後再排序其他國家的用戶。

請指導我如何在Laravel中做到這一點。 謝謝

+0

是什麼?你嘗試到現在? –

回答

0

你可以玩這個代碼。

$users = Users::with(['profiles' => function ($query) { 
    $query->where('country', 'USA'); 
    $query->orWhere('country', 'Poland') 
}])->get(); 
0

我還沒有檢查這個,但你可以試試這個解決方案。它會幫助你。

$users = Users::with(['profiles' => function ($query) { 
    $query->orderBy("country='your country name'", 'DESC') 
    $query->orderBy('country', 'ASC') 
}])->get(); 
0

您可以使用此

$users = Users::with(['profiles' => function ($query) use($country) { 
$query->orderBy(DB::raw('case when country="'.$country.'" then 1 else 2 end'))->orderBy('country', 'ASC'); 
}])->get(); 
相關問題