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

FounderStartup's avatar

How to filer records depending on radio values ?

I am developing a real estate site. I need to filter listings depending on the value of a radio button. How to handle this in my query ? The users have roles "broker" and "individual". I need to search listings depending on the roles of then user ( who has listed ).

My blade for radio button :

                     <div class="privacy-content">
                                                    <div class="media">
                                                        <div class="media-body">
                                                            <p class="font-roboto">Listings By All</p>
                                                        </div>
                                                        <label class="switch">
                                                            <input type="radio" name="radio1" value="all" checked=""><span class="switch-state"></span>
                                                        </label>
                                                    </div>
                                                    <div class="media">
                                                        <div class="media-body">
                                                            <p class="font-roboto">Listings By Brokers</p>
                                                        </div>
                                                        <label class="switch">
                                                            <input type="radio" name="radio1" value="broker" ><span class="switch-state"></span>
                                                        </label>
                                                    </div>
                                                    <div class="media">
                                                        <div class="media-body">
                                                            <p class="font-roboto">Listings By Individuals</p>
                                                        </div>
                                                        <label class="switch">
                                                            <input type="radio" name="radio1" value="individual"><span class="switch-state"></span>
                                                        </label>
                                                    </div>
                                                </div>

My controller is :


            $listingsforsale = Listings::with('listingtypename','project.builder','user','project','project.city','project.locality')
            ->whereHas('project', fn ($query) =>$query->when($request->project_city, fn ($query, $city) => $query->where('project_city', $city))->whereHas('city', fn ($query) => $query->where('status', 1)))
            ->where ('status', '1')
            ->where('dealstatus',1)
            ->where('project_id', '<>', NULL)
            ->where('expiry_date','>' , now())
            ->where('listingtype', 1)
            ->when($request->bedrooms, function ($query) use ($request) {$query->whereIn('bhk', [$request->bedrooms ]);})
            ->when($request->project_locality, function ($query) use ($request) {$query->whereHas('project.locality', fn($query)=>$query->where('project_locality', $request->project_locality));})
            ->orderBy('featured', 'DESC')
            ->withCount('response')
            ->orderBy('response_count', 'DESC')
            ->paginate(10)
            ->withQueryString();
0 likes
6 replies
webrobert's avatar

I’m really liking a match closure inside a when these days.

 ->when($request->radio1, fn ($q) => match($request->radio1) {
  ‘broker’ => $q ,// query for broker
‘individual’ => $q ,// query for individual 
default => $q
})

Forgive syntax I’m on my phone

1 like
FounderStartup's avatar

@webrobert Thanks a lot. But what I need is :

 ->whereHas('user', function ($q) {$q->whereRoleIs("broker");})

So how I will have this in the solution you have suggested ?

jlrdw's avatar

@FounderStartup you should consider authorization here rather than a radio selection, but just a suggestion.

Using some blade if's to determine who can do and see what.

1 like
FounderStartup's avatar

@jlrdw

There are two type of users in the system. Whose roles are "individual' and 'broker'. So when I display the listings page , I need to have a filter for 'listings by broker' and 'listings by individual'. I can do the work with the help of - if-then-else in controller , but in that case I will have to replicate around 200 lines of code for each role type :).

webrobert's avatar
Level 51

@FounderStartup

For the suggested code...

 ->when($request->radio1, fn ($query) => match($request->radio1) {
  	'broker' => $query->whereHas('user', fn ($q) => $q->whereRoleIs("broker") ),
	'individual' => $query->whereHas('user',  fn ($q) => $q->whereRoleIs("individual") ),
	default => $q
})

but there isn't much value to using match since the query is the same.

So this...

->when($request->radio1 != 'all', fn ($query) => 
	$query->whereHas('user', fn ($q) => $q->whereRoleIs($request->radio1) ) 
)

and of course be sure to validate that radio value is an acceptable choice.

1 like
FounderStartup's avatar

@webrobert Now that was an epic piece of code !!! Stay blessed and keep shining !! Thanks a lot for your time.

1 like

Please or to participate in this conversation.