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

FounderStartup's avatar

How to handle search page query ??

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 ?

0 likes
10 replies
Nakov's avatar

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.

1 like
FounderStartup's avatar

@Nakov @Nakov Thanks for your suggestion. Can you just show me at least next two checks....... That will be much clearer to me.

Nakov's avatar

@FounderStartup Same thing over and over:

$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)
    })
...

->when checks if the condition is true only then it performs the where() clause, otherwise it will be skipped, basically that's what you need if those are all checkbox values.

1 like
Sinnbeck's avatar

@FounderStartup The ->when() method actually gets the paramter it checks as the second argument, so you can use that if you want :) This is not a new answer as it does exactly the same as @nakovs, I am only giving a tip

$brokers = User::whereRoleIs('broker')
	->when($city, function($query, $value) {
		$query->where('city', $value);
    })
	->when($locality, function($query, $value) {
		$query->where('locality', $value);
    })
	->when($plots, function($query, $value) {
		$query->where('plots', $value);
    })
1 like
Nakov's avatar
Nakov
Best Answer
Level 73

@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.

1 like
FounderStartup's avatar

@Sinnbeck

Some logical issue :)

When I check 'rent' and 'plots' then only only those brokers show who have both the checkboxes checked in their profiles. What I need is to show brokers who have checked any of them. Which means I need o have OR in place of AND logic. What should I do ?

Please or to participate in this conversation.