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:
->modifyQueryUsing(fn (Builder $query) => $query->where('location_id', '=', Session::get('location_id')))
->filters([
SelectFilter::make('plan_id')
->label('Plan')
->placeholder('Filter by Plan')
->multiple()
->options(function () {
$locationId = Session::get('location_id');
return Plan::whereHas('subscriptionsPivot', function ($query) use ($locationId) {
$query->where('location_id', $locationId);
})->pluck('name', 'id')->toArray();
})
->query(function (Builder $query, array $data) : Builder {
// Check if plan_id is explicitly set in the filter
if ($data && isset($data['plan_id'])) {
return $query->whereHas('subscriptionsPivot', function (Builder $query) use ($data) {
$query->whereIn('plan_id', Arr::flatten($data));
});
}
return $query;
}),
])
->modifyQueryUsing(fn (Builder $query) => $query->where('location_id', '=', Session::get('location_id')))
->filters([
SelectFilter::make('plan_id')
->label('Plan')
->placeholder('Filter by Plan')
->multiple()
->options(function () {
$locationId = Session::get('location_id');
return Plan::whereHas('subscriptionsPivot', function ($query) use ($locationId) {
$query->where('location_id', $locationId);
})->pluck('name', 'id')->toArray();
})
->query(function (Builder $query, array $data) : Builder {
// Check if plan_id is explicitly set in the filter
if ($data && isset($data['plan_id'])) {
return $query->whereHas('subscriptionsPivot', function (Builder $query) use ($data) {
$query->whereIn('plan_id', Arr::flatten($data));
});
}
return $query;
}),
])
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:
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))
Jump to solution
4 Replies
Matthew
Matthew4w ago
if (Arr::get($data ?? [], 'plan_id', '')) {... Plenty of good reasons isset is out of favour.
sethbr11
sethbr11OP4w ago
@Matthew Thanks for the response, that's good to know about isset! Unfortunately, this solution ran into the same problem :/
Solution
sethbr11
sethbr114w ago
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))
Dennis Koch
Dennis Koch4w ago
isset() is out of favor? 🤔

Did you find this page helpful?