Answer the question
In order to leave comments, you need to log in
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
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.
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.
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 questionAsk a Question
731 491 924 answers to any question