我在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);
}
});
}
});
});
答案 0 :(得分:1)
喜欢
$query = SubCategoryLvl3::where('param', 'value');
if ($param1 == true) {
$query = $query->andWhere('param1', true);
}
if ($param2 == false) {
$query = $query->andWhere('param2', false);
}
$result = $query->all();
答案 1 :(得分: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);