Laravel

Laravel Eloquent When Condition With Example

Hello everyone, I hope you are doing well, In this article, we will get to know how to use when conditions in Laravel Eloquent with example.

Most of the time we have to use if else on the same eloquent, and it’s not the correct way of doing, because in that case you are calling the same query inside if and else statement. To get rid of this thing laravel has introduced When Condition.

Now let’s look for the below example where I am trying to get post related to the particular user, I thought you will also do in the below way, if you don’t know about the when condition.

Example:1 Getting Data by using if condition

In the below code I am trying to get Post related to the authenticated user and to get the authenticated user we have to Auth facade.

$posts = Post::select("*");
$userID = Auth::user()->id;
if ($userID) {
    $posts = $posts->where('user_id', $userID);
}
$posts = $posts->get();

Example:2 By using When Condition

Now we will do the same thing by using laravel eloquent when condition.

$userID = Auth::user()->id;
$posts = Post::select("*")
->when($userID), function ($query) use ($request) {
    $query->where('user_id', $userID);
})
->get();

Now the question is what to do in case of multiple if else condition, don’t worry about that I am here to rid of this thing also. see the below example.

Example:3 By using if-else condition

In the below example what I am trying to do is, I would like to sort it in ascending order based on published date, and if published date is not available then it will get data by created_at

 public function index(Request $request)
    {
        $posts = Post::select("*");
        if ($request->get('order_by') == "publish_date") {
            $posts = $posts->orderBy('publish_date', 'desc');
        } else {
            $posts = $posts->orderBy('created_at', 'desc');

        }
        $posts = $posts->get();
        dd($posts);
    }

Example:4 By using multiple when condition

Now we will do the same thing by using multiple when condition, Look for the below example.

public function index(Request $request)
{
    $posts = Post::select("*")
                ->when($request->get('order_by') == "publish_date", function ($query) {
                        return $query->orderBy('publish_date', 'desc');
                    }, function ($query) {
                        return $query->orderBy('created_at', 'desc');
                    })
                    ->get();

    dd($posts);
}

It’s not just a prettier way to write the same “IF” but is also a great way to organize conditional queries.

I hope now you understood, that how we can use laravel eloquent when condition, then how about giving me 5 stars. Thanks.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button