Laravel 按特定值排序结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26045934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel ordering results by specific values
提问by Darryl Chapman
I have this line of code which gets results from a database:
我有这行代码可以从数据库中获取结果:
'clanMembers' => User::find(Auth::user() -> clan_id) -> where('clan_id', '=', Auth::user() -> clan_id) -> orderBy('username') -> get()
Currently it orders by the username. I wish to change this to order by the clan_rank field. This clan_rank field will only ever contain 3 different values. These are:
目前它按用户名订购。我希望将其更改为按 clan_rank 字段排序。这个 clan_rank 字段将只包含 3 个不同的值。这些是:
1. Owner
2. Admin
3. Member
I wish to have my results ordered with Owners first, then Admin, then members. How can I change my line of code to achieve these results?
我希望我的结果首先与所有者排序,然后是管理员,然后是成员。如何更改我的代码行以实现这些结果?
回答by Marcin Nabia?ek
You need to use orderByRaw
:
你需要使用orderByRaw
:
instead of orderBy
part you should use
而不是orderBy
你应该使用的部分
orderByRaw("FIELD(clan_rank , 'Owner', 'Admin', 'Member') ASC");