2017-05-26 46 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(); 
相关问题