【问题标题】:How to dynamically append multiple conditions to laravel query?如何动态地将多个条件附加到 laravel 查询?
【发布时间】:2019-01-25 13:44:25
【问题描述】:

我在 Laravel 中遇到了一个问题。我正在尝试将多个条件附加到查询中。换句话说,我想根据特定参数是否具有值来添加多个 where 条件。如果参数有值,则应附加。我正在制作产品过滤器,目前我已经添加了产品条件以及最低和最高价格查询条件。我希望他们两个一起工作。我已经尝试过另一个对象。

控制器功能

public function FilterProducts(Request $request){
        $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 代码

$(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);
                    }
                });
            }
        });
});

【问题讨论】:

标签: jquery laravel


【解决方案1】:

如果你使用 Laravel 5.2+,你也可以使用 when() 助手。像这样的东西可以解决问题。

$products = DB::table('products')
    ->where('category_level', 3)
    ->when($product_condition, function ($query) use ($product_condition) {
        return $query->where('product_condition', $product_condition);
    })
    ->when($selling_price, function ($query) use ($selling_price) {
        return $query->where('selling_price', $selling_price);
    })
    ->when($min_price && $max_price, function ($query) use ($min_price, $max_price) {
        return $query->whereBetween('selling_price',[$min_price, $max_price]);
    })
    ->whereIn('category',$category_level3_id)
    ->paginate(40);

【讨论】:

  • 非常感谢。但是我需要做一个改变。我没有在 jQuery 中传递额外的参数。
【解决方案2】:

这样

$query = SubCategoryLvl3::where('param', 'value');
if ($param1 == true) {
    $query = $query->andWhere('param1', true);
}
if ($param2 == false) {
    $query = $query->andWhere('param2', false);
}
$result = $query->all();

【讨论】:

  • 你用了all方法我也想用paginate方法
  • $query->paginate(10);
猜你喜欢
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
  • 2020-01-22
  • 1970-01-01
相关资源
最近更新 更多