How to dynamically append multiple conditions to Laravel query?

Posted 5 months ago by nitinjohnson

I have an issue that I am facing in Laravel. I am trying to append multiple conditions to a query. In other words I want to add multiple where conditions based on if a particular parameter has value or not. If the parameter has value it should be appended. I am making a product filter and currently I have added product condition and min and max price query conditions. I want both of them to work together.

Controller Function

        $product_condition=$request->get('product_condition');
        $min_price=$request->get('min_price');
        $max_price=$request->get('max_price');
        $page_slug=$request->get('page_slug');
        $products=null;
        $products2=null;
        $query_condition=null;

        $getCatlvl1=Category::where([
            ["category_slug","=",$page_slug]
        ])->get();

        $getCatlvl2=SubCategory::where([
            ["category_slug","=",$page_slug]
        ])->get();

        $getCatlvl3=SubCategoryLvl3::where([
            ["category_slug","=",$page_slug]
        ])->get();

        if(count($getCatlvl1)>0){
            $category_level1_id=null;
            $category_level2_id=null;
            $category_level3_id=null;

            foreach($getCatlvl1 as $gc){
                $category_level1_id=$gc->id;
            }

            $get_child_cat_level2=SubCategory::where('parent_category',$category_level1_id)->get();
            foreach($get_child_cat_level2 as $gc2){
                $category_level2_id[]=$gc2->id;
            }

            $get_child_cat_level3=SubCategoryLvl3::whereIn('parent_category',$category_level2_id)->get();
            foreach($get_child_cat_level3 as $gc3){
                $category_level3_id[]=$gc3->id;
            }

            $products = DB::table('products')
                ->where('category_level', 3)
                ->whereIn('category',$category_level3_id);

            $products2 = DB::table('products')
                ->where('category_level', 3)
                ->whereIn('category',$category_level3_id);

            if(!empty($product_condition)){
                $products->where('product_condition',$product_condition);
                $products2->where('product_condition',$product_condition);
            }

            if(!empty($min_price) && !empty($max_price)){
                $products->whereBetween('selling_price',[$min_price,$max_price]);
                $products2->whereBetween('selling_price',[$min_price,$max_price]);
            }

            $products = $products->paginate(40);
            $products2 = $products2->get();

            $filter_text_for_page=null;

            if(!empty($product_condition)){
                $filter_text_for_page.='<p class="mytagcloud"><strong>Condition:</strong> '.ucwords($product_condition).'</p>';
            }

            if(!empty($min_price) && !empty($max_price)){
                $filter_text_for_page.='<p class="mytagcloud"><strong>Price Range:</strong> '.$min_price.'-'.$max_price.'</p>';
            }

            return view('frontend.pages.category_load', [
                'products' => $products,
                'products_count'=>$products2->count(),
                'page_slug'=>$page_slug,
                'category_level_column'=> 2,
                'product_card_class '=> 'six_cols_card',
                'page_filter_text'=> $filter_text_for_page,
            ])->render();

        } else if(count($getCatlvl2)>0){

        } else if(count($getCatlvl3)>0){

        }

    } ```

jQuery Code

$(document).ready(function(){
//Filters
        $(".product_condition_filter").click(function(e){
            var selected_condition=$(this).val();
            $.ajax({
                url:"{{ route('category-page-filter') }}",
                type:"post",
                async:false,
                data:{product_condition:selected_condition, page_slug:$("#page_slug").val()},
                success:function(res){
                    $(".category_products_box").html(res);
                }
            });
        });

        $( "#price_range_slider" ).slider({
            range: true,
            min: 100,
            max: 50000,
            values: [ 1000, 5000 ],
            slide: function( event, ui ) {
                $(".slide_min_val").html(ui.values[0]);
                $(".slide_max_val").html(ui.values[1]);

                $.ajax({
                    url:"{{ route('category-page-filter') }}",
                    type:"post",
                    async:false,
                    data:{min_price:ui.values[0],max_price:ui.values[1], page_slug:$("#page_slug").val()},
                    success:function(res){
                        $(".category_products_box").html(res);
                    }
                });
            }
        });
}); ```

Please sign in or create an account to participate in this conversation.