V
V
Vladislav2018-05-29 16:35:05
Laravel
Vladislav, 2018-05-29 16:35:05

How to get data from DB sorted by a specific field?

How, let's say, when you click on the Status button, make sure that all data is sorted by the value Unanswered?
Those. all the lines that contain Unanswered were at the very top?
On the screen is an example of the page prntscr.com/jo2p1a

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
iljaGolubev, 2018-05-29
@vos_50

Let's say your data is stored in MySQL in the questions table . To get all the data in PHP with the values ​​of the status field sorted in the order you specify, do this:

App\Question::orderByRaw("FIELD(status,'Без ответа,'Скрыт')")->get();
you have to list all the possible statuses 'Unanswered,'Hidden' and so on - otherwise the focus will fail.

A
arab789, 2018-05-31
@arab789

If you are building a table, then laravel-datatables will help you, it is very easy to use and out of the box there are sorting by columns, pagination and search.

A
Alexander Aksentiev, 2018-05-29
@Sanasol

Let's write such a functionality on the front that it sends a sort field to the server and in which direction to sort.
And on the backend, let's use orderBy at this moment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question