Laravel查询中的数组结果

问题描述 投票:1回答:1

我想在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'));
}

如果我选择品牌lenovolg我只得到lenovo品牌的结果。

php laravel
1个回答
2
投票

使用whereIn

Product::whereIn('brand_id', $brand)->get();
© www.soinside.com 2019 - 2024. All rights reserved.