Skip to content
Advertisement

How to always fetch a special category item data first in Laravel

Suppose I have a table named machine with 3 columns(id, name, company).

There are 5 data in that table.

Here A,B,C,D,E are the machine name and XX,XY,YY are the company name.

Now I want to fetch data according to company wise and there a specific company’s data will come always first. For that I use groupBy with company.

SQL

Result

But I want to retrieve data where XX company’s data will always come first like below format

How to do that ? Anybody can help please ? Thanks in advance.

Advertisement

Answer

You can order by whether the company name is XX or not, using a boolean condition in orderbyRaw:

If you want to sort multiple companies in order, use a CASE expression:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement