Laravel ordering results by specific values

孤街醉人 提交于 2019-12-09 18:21:47

问题


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:

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?


回答1:


You need to use orderByRaw:

instead of orderBy part you should use

orderByRaw("FIELD(clan_rank , 'Owner', 'Admin', 'Member') ASC");


来源:https://stackoverflow.com/questions/26045934/laravel-ordering-results-by-specific-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!