To address the issue of the filter indicator not showing in your Filament table, you need to ensure that the filter state is correctly set and that the indicator function is properly configured to reflect the active state of the filter.
Here's a revised version of your code with some adjustments to ensure the filter indicator works as expected:
use Illuminate\Database\Eloquent\Builder;
use App\Models\ScientificName;
use App\Models\CountryName;
use Filament\Tables\Filters\Filter;
use Filament\Forms\Components\Select;
Filter::make('Distribution')
->form([
Select::make('distrib')
->options(function () {
return CountryName::query()
->whereNotNull('country_name')
->where('country_code', 'cycads native')
->orderBy('country_name', 'asc')
->pluck('country_name', 'country_name')
->toArray();
})
])
->query(function (Builder $query, array $data): Builder {
return $query->when(
isset($data['distrib']),
function (Builder $query) use ($data) {
return $query->where('distrib', 'LIKE', '%' . $data['distrib'] . '%');
}
);
})
->indicator(function ($state) {
return isset($state['distrib']) && $state['distrib'] ? 'Country: ' . $state['distrib'] : null;
});
Key Adjustments:
-
Indicator Function: Ensure the
indicatorfunction checks if thedistribkey is set and not null. This ensures the indicator text is only shown when the filter is active. -
Filter State: The
indicatorfunction should correctly reflect the state of the filter by checking thedistribkey in the$statearray.
Explanation:
-
Form Configuration: The
Selectcomponent is configured to fetch options from theCountryNamemodel. -
Query Modification: The
querymethod modifies the query based on the selecteddistribvalue. -
Indicator: The
indicatorfunction returns a string indicating the active filter state, which will be displayed in the Filament table.
By ensuring the indicator function correctly checks the filter state, the indicator should now display when the filter is active.