Toggle Filter only used when On?

I have a table filter like so
Filter::make('criteria_id')
->label('Has Criteria')
->query(fn (Builder $query): Builder => $query->whereHas('criteria'))
->toggle(),
Filter::make('criteria_id')
->label('Has Criteria')
->query(fn (Builder $query): Builder => $query->whereHas('criteria'))
->toggle(),
. i only want it applied when its toggled on and it do nothing when its off. How can i do that?
19 Replies
Jordy
Jordy16mo ago
On what version are you? I have a simular toggle and it work fine My guess is your relationship with criteria may not be defined?
Saifallak
Saifallak16mo ago
maybe use tenary filter ? it has true and false and blank state
Saifallak
Saifallak16mo ago
wyChoong
wyChoong16mo ago
in your closure,
$query->when($state, fn($query) => $query->whereHas('criteria))
$query->when($state, fn($query) => $query->whereHas('criteria))
something like this? might need to adjust the $state param
Mark Chaney
Mark ChaneyOP16mo ago
that does partly work, but query string still has ?tableFilters[criteria_id][isActive]=0 when its off
awcodes
awcodes16mo ago
->when($state !== 0, fn….) Wrap your where inside a when. Also keep in mind that the toggle doesn’t have a null. It’s always true or false when it’s interacted with.
Mark Chaney
Mark ChaneyOP16mo ago
Right, thats the problem. I think for most people, thats not how they want a toggle filter to work. Could easily be a wrong assumption
awcodes
awcodes16mo ago
well, it's a binary input. on / off, true / false, makes sense to me it's not about the filter is about the input your using for the filter.
Mark Chaney
Mark ChaneyOP16mo ago
yes, on/off. I either want hte filter on or i want it off.
awcodes
awcodes16mo ago
if you need null / true / false, then you'll need a select since it provides for more than 2 options Think about it this way, say you have a toggle filter for published / draft, the on / off mentality there would break since it wouldn't run the filter for items set to draft. you're not turning the filter on or off, you're toggle the state of the records to be filtered
Mark Chaney
Mark ChaneyOP16mo ago
no i get that, im just not looking for two states. I either want to filter by drafts only or i dont want to filter at all I understand that. Thats just not my desired result
awcodes
awcodes16mo ago
then wrap it in a when to check the state of the toggle. it's operating exactly like it should, you're just trying to use it in a different way, so you'll have to make checks against it's state to modify the outcome
Mark Chaney
Mark ChaneyOP16mo ago
i dont think that when() will affect the querystring when its off, which is my desired outcome as mentioned 15 minutes ago 😛
awcodes
awcodes16mo ago
you're right it won't affect the query string. but changing the query string isn't what you need.
Mark Chaney
Mark ChaneyOP16mo ago
im already getting the proper results
awcodes
awcodes16mo ago
then why are we discussing this. LOL
Mark Chaney
Mark ChaneyOP16mo ago
i have no idea as i already confirmed that 15 minutes ago that the filtering is working properly, it just doesnt have the querystring state that i would prefer. NOw when ever the table is loaded, the querystring always has a default state of ?tableFilters[criteria_id][isActive]=0. Its not a huge deal, but i would prefer not ot have it if a filter really isnt applied, which it isnt
awcodes
awcodes16mo ago
10-4 guess i missed the comment where you said it was giving you the correct results. sorry.
Want results from more Discord servers?
Add your server