Navigate back to the homepage

Laravel order by case when example

June 15th, 2021 · 1 min read

Hello Friends 👋,

Welcome To Infinitbility! ❤️

This article will help you to write order by cases query like when we use Order by case when query in sql then how we use or implement In laravel.

Let’s start today’s article Laravel order by case when example

The Laravel provide orderByRaw function to implement case when query and we are going to use.

First we have to understand why we need case when in query.

Assume you have multiple key in single column example you have gender column and you are storing Male, Female, Others.

You want to show data of first male user than female and after that others users, then you have to use case when query where you can define which user you want show first, second, and last.

Let’s understand with example.

1$users = DB::table("users")->orderByRaw("CASE WHEN gender = 'Male' then 1 WHEN gender = 'Female' then 2 WHEN gender = 'Others' then 3 Else 4 END DESC")->get();

Thanks for reading…

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide



More articles from Infinitbility

How to open Archive build list in Xcode

Archive navigation solution

June 13th, 2021 · 1 min read

Archive uploading error to App Store using Xcode

how to solve archive uploading error to App Store using Xcode

June 12th, 2021 · 1 min read
© 2020–2022 Infinitbility
Link to $ to $ to $ to $ to $ to $mailto:[email protected]