Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

maaz's avatar
Level 2

How to query Search with related model.

Hello everyone. I have a search box where the user will enter the business name and a dropdown where the user will select the state/province as shown in the dropdown. The business model has a relationship with the Province model. How I can do a search based on province only. if user leaves a search bar empty and only select province I want to show all the businesses based on states/provinces. here is my: Index view:

 <form method="get" action="{{route('search.business')}}">
                        <div class="inner-form">
                            <div class="input-field first-wrap">
                                <input type="text" placeholder="What are you looking for?" name="search" />
                            </div>
                            <div class="input-field second-wrap">
                                <select name="search" id="" class="form-control" style="height:inherit;">
                                    @foreach ($provinces as $province)
                                    <option value="{{$province->id}}">{{$province->name}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="input-field third-wrap">
                                <button class="btn-search" type="submit">Search</button>
                            </div>
                        </div>
                    </form>

my search controller

   public function search(Request $request)
    {
        $businesses = Business::with('provinces')
            ->where('name', 'LIKE', '%' . $request->input('search') . '%')
            ->paginate(10);
        // dd($businesses);
        return view('front-end.search-details', compact('businesses'));
    }

plz, tell me a query for the province to this is my first time to use search functionality that's why I am stuck here.

0 likes
10 replies
MichalOravec's avatar
Level 75
$businesses = Business::with('provinces')->whereHas('provinces', function ($query) use ($request) {
    $query->where('id', $request->search);
})->paginate(10);

But probably will be better when you change search to province_id

select name="province_id">

Then id will be in

$request->province_id
1 like
MichalOravec's avatar

@maaz Ou sorry I didn't see that you have there also input.

$businesses = Business::with('provinces')->whereHas('provinces', function ($query) use ($request) {
    $query->where('id', $request->province_id);
})->where('name', 'LIKE', "%{$request->search}%")->paginate(10);
1 like
maaz's avatar
Level 2

@michaloravec it's okay. Now the thing is when I didn't select the province and just enter the business name it doesn't give the output, but when I select both it shows me the required output.

maaz's avatar
Level 2

Thanks a lot, it works fine now :) Can you highlight what is happening in the query @michaloravec ? let me understand this I am getting this a little bit but not so much 🙂

maaz's avatar
Level 2

Okay, thanks again. You always help me here :) I will go through the documentation :)

MichalOravec's avatar

@maaz Yeah in the documentation you find a lot answers for your questions :)

1 like

Please or to participate in this conversation.