Filter Not Returning Anything When Nothing is Selected
I am trying to filter my contacts based on what subscription type they have, which is stored in a different table but has foreign key relationships. Here is my code so far:
The main problem has been the
if ($data && isset($data['plan_id']))
line. If I just have if($data)
, it seems to always evaluate to true, even if I don't have a filter option selected, which means the filters themselves work but when nothing is selected nothing shows up. If I try the line as it is in my code, everyone shows up when nothing is selected—even those without a plan, which is what I want—but isset($data['plan_id'])
seems to always evaluate to false, so then the filters don't do anything. I'm pretty new to Filament. Any tips?Solution:Jump to solution
For anyone else with the same problem, I guess the filter just gets passed as "values" instead of the actual type of value, which in my case would have been "plan_id". Here's what a log showed:
Filter data: {"values":[]}
. From there, I adjusted my code to get the values with $planIds = Arr::get($data, 'values', []);
and then check if there was anything there or not with if (!empty($planIds))
4 Replies
if (Arr::get($data ?? [], 'plan_id', '')) {...
Plenty of good reasons isset
is out of favour.@Matthew Thanks for the response, that's good to know about isset! Unfortunately, this solution ran into the same problem :/
Solution
For anyone else with the same problem, I guess the filter just gets passed as "values" instead of the actual type of value, which in my case would have been "plan_id". Here's what a log showed:
Filter data: {"values":[]}
. From there, I adjusted my code to get the values with $planIds = Arr::get($data, 'values', []);
and then check if there was anything there or not with if (!empty($planIds))
isset()
is out of favor? 🤔