Laravel order results for specific values

I have this line of code that gets the results from the database:

'clanMembers' => User::find(Auth::user() -> clan_id) -> where('clan_id', '=', Auth::user() -> clan_id) -> orderBy('username') -> get()

He is currently ordering a username. I want to change this to order in the clan_rank field. This clan_rank field will contain only 3 different values. It:

1. Owner
2. Admin
3. Member

I want my results to be ordered from the Owners first, then the Administrator, then the members. How can I change my line of code to achieve these results?

+4
source share
1 answer

You need to use orderByRaw:

instead of part orderByyou should use

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

All Articles