我想基于特定值对用户进行排序。例如,我有Users table
并与Profiles Table
有关系。在Profiles table
我已保存Country (string)
的用户。根据Laravel中的特定值(字符串)对用户进行排序
现在我想先排序特定的国家用户,然后再排序其他国家的用户。
请指导我如何在Laravel中做到这一点。 谢谢
我想基于特定值对用户进行排序。例如,我有Users table
并与Profiles Table
有关系。在Profiles table
我已保存Country (string)
的用户。根据Laravel中的特定值(字符串)对用户进行排序
现在我想先排序特定的国家用户,然后再排序其他国家的用户。
请指导我如何在Laravel中做到这一点。 谢谢
你可以玩这个代码。
$users = Users::with(['profiles' => function ($query) {
$query->where('country', 'USA');
$query->orWhere('country', 'Poland')
}])->get();
我还没有检查这个,但你可以试试这个解决方案。它会帮助你。
$users = Users::with(['profiles' => function ($query) {
$query->orderBy("country='your country name'", 'DESC')
$query->orderBy('country', 'ASC')
}])->get();
您可以使用此
$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();
是什么?你尝试到现在? –