Laravel

How to use if condition in laravel eloquent

Hello buddy, in this article, we will get to know how we can use if condition in laravel eloquent, sometimes it’s needed to use condition in the eloquent.

Mostly this type of condition is required when we are working on the filters, see the below example, how we can do this.

 /**
     * This function execute during the filter change
     * @param object $request
     * @return array 
     */
    public function filterData($request)
    {
          ProductFlat::when($request->has('priceRange'), function ($query) use ($request) {
                if ($request->priceRange > ProductFlat::getMinPrice()) {
                    $query->whereBetween('price', [ProductFlat::getMinPrice(),$request->priceRange]);
                }
            })
            ->when($request->has('priceRangeDefined'), function ($query) use ($request) {
                if ($request->priceRangeDefined != 'all') {
                    $explodePriceMinMax = explode('-', $request->priceRangeDefined);
                    $query->whereBetween('price', [$explodePriceMinMax[0], $explodePriceMinMax[1]]);
                }
            })
            ->when($request->has('categories'), function ($query) use ($request) {
                if ($request->categories != 'all') {
                    $query->join('product_categories as ct', 'ct.product_id', '=', 'product_id');
                    $childCategory = Category::where('parent_id',$request->categories)->pluck('id')->toArray();
                    if(count($childCategory) > 0) {
                        $query->whereIN('ct.category_id', $childCategory);
                    } else {
                        $query->where('ct.category_id', $request->categories);
                    }
                }
            })
            ->groupBy('product_id')
            ->select('product_id', 'description', 'name', 'price', 'url_key as slug', 'id', 'thumbnail as image', 'special_price', 'special_price_from', 'special_price_to')
            ->orderby('id', 'DESC')
            ->get();
    }

Code Explanation

Now I will explain, what I have done in the above code, as I previously said mostly we need to implement if else condition when we are using filter something like that in our frontend.

I have applied the when condition on my product flat table based on the request, let’s suppose someone has to want’s to see the product between the particular price range and someone wants to see the category between the particular price range.

So for the above type of condition if we don’t use the when condition in our code we must have to call the multiple time of eloquent query, which breaks the DRY concept Don’t repeat yourself.

On the other side, we can also use if-else inside the when condition as per the requirement.

So I think now you understood how this when condition works and how you can implement this with your code.

Thanks for reading this small article over the when condition, still confused please add a comment, we will try to reply you as soon as possible.

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