F
F
flafy42019-12-10 10:18:19
Laravel
flafy4, 2019-12-10 10:18:19

How to sort selection in Laravel by value in table?

There is a table:

| id | name    | role  |
|----|---------|-------|
| 1  | John    | admin |
| 2  | Tom     | user  |
| 3  | Paul    | user  |
| 4  | Mikle   | moder |
| 5  | Lindsey | admin |

How to sort so that the field values role​​are in this order when displayed:
1. admin
2. moder
3. user
That is, the collection of users should look like this if you look at the id: [1, 5, 4, 2, 3]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolai Smirnov, 2019-12-10
@Bzdykin

Read about sorting in the documentation. Everything works the same way with eloquent.

D
Dmitry, 2019-12-10
@thewind

In sql it looks like ORDER BY FIELD(role, ‘admin’,’moder’,’user’)
you need to find the same feature in laravel

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question