Default Filter Value Not Working in Laravel Views
I’m trying to add a default value to a filter in Laravel Views, but it’s not working as expected. Here’s my current implementation:
GruposActiveFilter class:
use LaravelViewsFiltersFilter;
use IlluminateDatabaseEloquentBuilder;
class GruposActiveFilter extends Filter
{
public function apply(Builder $query, $value, $request): Builder
{
return $query->where('Activo', $value);
}
public function options(): array
{
return [
'Activo' => 1,
'Inactivo' => 0,
];
}
}
In my ListView:
protected function filters()
{
return [
new GruposActiveFilter,
];
}
What I’ve tried:
I’ve attempted to add a default()
method to the filter class, but it doesn’t seem to be recognized:
public function default()
{
return 1;
}
Expected behavior:
I expect the filter to default to ‘Activo’ (value 1) when the page is first loaded.
Actual behavior:
The filter is not applying any default value, and I have to manually select a filter option each time.
How can I set a default value for my GruposActiveFilter
in Laravel Views so that it’s applied automatically when the page loads?
Any help or guidance would be greatly appreciated!