1

I want to get products of selected brands can be 1 or more (using checkbox) in laravel but it returns result of only one of my selected brands.

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

If I choose brands lenovo and lg I only get result of lenovo brand.

1 Answer 1

2

Use the whereIn:

Product::whereIn('brand_id', $brand)->get();
Sign up to request clarification or add additional context in comments.

3 Comments

Invalid argument supplied for foreach()
@djhru please show result of dd($request->brands);
it's working i'm sorry. (please wait 10 min till i'm allowed to accept your answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.