0

I made a filter to find businesses

There are several cities to choose from

enter image description here

Cities store on city_id column

The filter looks like this

<select id="city" multiple name="city[]">
  @foreach($cities as $city)
    <option value="{{ $city->name }}">{{ $city->name }}</option>
  @endforeach
</select>

All request

dd($request->all()) 

shows me this

enter image description here

I build query for franchise or business like this

if ($request->has('fb')) {
  $businessesQuery->where('fb', $request->fb);
}

I try to build query like this but it's not works

if ($request->has('city[]')) {
    $typeArray = explode(",", $request->city[]);
    $businessesQuery->whereIn('city_id', $typeArray);
}

Help me solve this issue, I would be very grateful!

1
  • You should be able to do just: ->whereIn('city_id', $request->city) since whereIn() accepts arrays Commented Feb 27, 2022 at 10:12

1 Answer 1

1

Your html must use city_id as a value and not the city name. Because you're trying to search using the id not name.

    <option value="{{ $city->id }}">{{ $city->name }}</option>

explode function takes a string as in input and returns an array. In your case your city[] request value is already an array visible from the dump. You should use it directly. Something like this.

if ($request->has('city')) {
    $businessesQuery->whereIn('city_id', $request->input('city');
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm quite sure that it should be: input('city') and not input('city[]'). When adding [] to an input name, PHP will simply store it in the name (without the []) but as an array.
@M.Eriksson you're right. Thanks! I will update my 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.