我希望在laravel中获得所选品牌can be 1 or more (using checkbox)
的产品,但它只返回我所选品牌之一的结果。
Form
<form action="{{route('mysearch')}}" method="post">
{{csrf_field()}}
<div class="filter-content">
<div class="checkbox">
@foreach($brands as $brand)
<label for="brands">
<input name="brands[]" type="checkbox" value="{{$brand->id}}">
{{$brand->title}}
</label>
@endforeach
</div>
</div>
<button type="submit" class="btn btn-danger">submit</button>
</form>
function
public function mysearch(Request $request) {
$brands = Brand::OfStatus('Active')->get();
$brand = Input::has('brands') ? Input::get('brands') : [];
$products = Product::where('brand_id', '=', $brand)->get();
return view('front.search', compact('products', 'brands'));
}
如果我选择品牌lenovo
和lg
,我只能获得lenovo
品牌的结果。
答案 0 :(得分:2)
使用whereIn
:
Product::whereIn('brand_id', $brand)->get();