4电子商务网站
我试图过滤我的数据,但我做错了
每个过滤器工作正常,如颜色品牌价格,但当我在性别部分之后我dd($products)
时过滤性别的返回数据
但是当我在返回视图之前dd($products)
时返回0数据意味着什么
这是我的form
{!! Form::open(['url' => 'category/' . $tag . '/filter', 'files'=>false, 'id' => 'color_form']) !!}
Price Filter
<input name="price" id="ex2" type="text" class="span2" value="" data-slider-min="10" data-slider-max="2000" data-slider-step="5" data-slider-value="[250,450]" onchange="checkBoxHandler()"/>
<li class="">
<input type="checkbox" name="gender[]" id="men" value="male" onchange="checkBoxHandler()"> Men's
</li>
<li class="">
<input type="checkbox" name="gender[]" id="woman" value="female" onchange="checkBoxHandler()"> Woman's
</li>
@foreach($colors as $color)
<input type="checkbox" name="color[]" id="color{{ $color->name }}" value="{{ $color->name }}" onchange="checkBoxHandler()">
<div style="width:25px; height:25px; background:{{ $color->name }}; border:1px solid black; margin:0px 3px 0px 3px; display:inline-block;"></div>   {{ $color->name }}
@endforeach
@foreach($brands as $brand)
<input type="checkbox" name="brand[]" id="brand{{ $brand->name }}" value="{{ $brand->name }}" onchange="checkBoxHandler()">
{{ $brand->name }}<br>
@endforeach
@foreach($sizes as $size)
<input type="checkbox" name="size[]" id="size{{ $size->name }}" value="{{ $size->name }}" onchange="checkBoxHandler()">
{{ $size->name }}<br>
@endforeach
{!! Form::close() !!}
这是我的controller
public function newinindex(Request $request){
if(request()->has('gender')||request()->has('price')||request()->has('color')||request()->has('brand')){
if (request()->has('gender')) {
$products = Product::orderBy('created_at', 'desc')->where('gender', request('gender'))->newin()->paginate(20);
}
if (request()->has('price')) {
$products = Product::orderBy('created_at', 'desc')->newin()->whereBetween('price', explode(',', $request->price))->paginate(20);
}
if (request()->has('color')) {
$products = Product::orderBy('created_at', 'desc')->newin()->whereHas('colors', function ($query) {
$query->where('name', request('color'));
})->paginate(20);
}
if (request()->has('brand')) {
$products = Product::orderBy('created_at', 'desc')->newin()->whereHas('brands', function ($query) {
$query->where('name', request('brand'));
})->paginate(20);
}
}
else {
$products = product::orderBy('created_at', 'desc')->newin()->paginate(20);
}
$sizes = size::orderBy('created_at', 'desc')->paginate(20);
$colors = color::orderBy('created_at', 'desc')->paginate(20);
$brands = brand::orderBy('created_at', 'desc')->paginate(20);
return view('newin.index' ,compact('products', 'brands', 'colors', 'sizes'));
}
答案 0 :(得分:0)
性别字段是一个数组!请参阅以下链接并尝试使用字段名称中的值。
答案 1 :(得分:0)
您正在使用gender
复选框作为数组。修改您的controller
$products = Product::orderBy('created_at', 'desc')->whereIn('gender', request('gender'))->newin()->paginate(20);
whereIn
方法验证给定列的值是否包含在给定数组中。
希望它有用。