Here is an idea:
$brokers = User::whereRoleIs('broker')
->when($city, function($query) use ($city) {
$query->where('city', $city)
})
...
do that for each of your checks.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
On then search page I have few filters : The controller is :
$brokers = User::whereRoleIs('broker')
->where('city', $city)
->where('locality', $locality)
->where('plots', $plots)
->where('apartments', $apartments)
->where('agricultureland', $agricultureland)
->where('internationalproperties', $internationalproperties)
->where('industrial', $industrial)
->where('commercial', $commercial)
->where('residential', $residential)
->where('rent', $rent)
->where('buysell', $buysell)
->where('primary', $primary)
->where('secondary', $secondary)
->orderBy('id', 'ASC')
->paginate(25)
->withQueryString();
Default values of all checkboxes is Unchecked.
If I select a CITY and the click SEARCH all the brokers of the city should show. If I select CITY and then locality from dropdown then all the brokers of that locality of that city should show.
Since all the checkboxes are unchecked so in the above queries only those brokers are showing who have not checked any option in their profile though I need to show all of them ( as I have selected any checkbox ).
But once I check any box in the filter then only those brokers should show who has checked that checkbox. He may or may not have checked any other checkbox.
What will be my query ?
@FounderStartup okay, here you go:
$brokers = User::whereRoleIs('broker')
->when($city, function($query) use ($city) {
$query->where('city', $city);
})
->when($locality, function($query) use ($locality) {
$query->where('locality', $locality);
})
->when($plots, function($query) use ($plots) {
$query->where('plots', $plots);
})
...
:) you must know that I don't run your code on my machine, so little things like this can be missed. You've got to learn what the error says and try to find the fix my friend.
Please or to participate in this conversation.