Search code examples
sqleloquentsql-order-bylaravel-4.2

how to retrieve a record always first and rest of the records using orderby?


I am new to Laravel. I am trying to retrieve some records from a database. I need one of the row to be always in the first place, and rest in alphabetical order.Like this :

CategoryID     Category
---------------------------
54            New Products
1             Amino Acids
3             b---
34            c----
4             d---

How should I rewrite my code to achieve this?

$db_categories = Category::get();

Thanks in advance.


Solution

  • You can use this:

    Category::orderBy(DB::raw("IF(category = 'New Products', 0, 1), Category"))->get();