guiu / filament-filter-presets
A Filament plugin to save and load filter presets
Requires
- php: ^8.1
- filament/filament: ^3.0
- illuminate/support: ^10.0|^11.0
- laravel/framework: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpunit/phpunit: ^10.0
README
This package allows users to save and load predefined filter configurations in Filament resources. It is especially useful when:
- You need quick access to specific combinations of filters that you frequently use
- You want to create predefined filters for different scenarios or use cases
- You wish to set default filters that are automatically applied when a list is loaded
- You want to share filter configurations among different users of the system
With this module, you can:
- Save your custom filters with a name and description
- Quickly load previously saved filters
- Set default filters
- Manage your saved filters (delete, edit, set as default)
Step by Step
Prerequisites
Before installing this package, make sure your project meets the following requirements:
- PHP 8.1 or higher
- Laravel 10.0 or higher
- Filament 3.0 or higher
Installation
- Install the package via composer:
composer require guiu/filament-filter-presets
- Publish and run the migrations:
php artisan vendor:publish --tag=filament-filter-presets-migrations php artisan migrate
- Optionally, you can publish the translations:
php artisan vendor:publish --tag=filament-filter-presets-translations
Usage
- Add the
HasFilterPresets
trait to your List Resource page (not in the main Resource):
namespace App\Filament\Resources\YourResource\Pages; use Filament\Resources\Pages\ListRecords; use Guiu\FilamentFilterPresets\Traits\HasFilterPresets; class ListYourResource extends ListRecords { use HasFilterPresets; protected function getHeaderActions(): array { return [ Actions\CreateAction::make(), ...static::getFilterPresetHeaderActions(), ]; } }
Important:
- The trait must be added to the List class that extends
ListRecords
- Add the filter preset actions in the
getHeaderActions()
method (NOT in the table configuration) - Do NOT add the trait to the main Resource class
Features
- Save filters with name and description
- Mark filters as default
- Load saved filters
- Manage filters (delete, set as default)
Advanced Customization
Button Customization
You can customize the appearance of buttons by overriding the following methods:
protected static function getSaveFilterButtonLabel(): string { return 'Save filter'; } protected static function getLoadFilterButtonLabel(): string { return 'Load filter'; } protected static function getSaveFilterModalTitle(): string { return 'Save filter configuration'; } protected static function getLoadFilterModalTitle(): string { return 'Load filter configuration'; }
Integration with Other Plugins
You can integrate this package with other Filament plugins. For example, with the export plugin:
use Filament\Tables\Actions\ExportAction; public static function table(Table $table): Table { return $table ->headerActions([ ...static::getFilterPresetActions(), ExportAction::make() ->beforeExporting(function (Builder $query) { // Apply saved filters before exporting if ($preset = static::getDefaultPreset()) { $query = static::applyPresetFilters($query, $preset); } return $query; }) ]); }
Custom Actions
You can add custom actions to presets:
protected static function getFilterPresetActions(): array { return [ ...parent::getFilterPresetActions(), Action::make('duplicatePreset') ->label('Duplicate') ->icon('heroicon-o-duplicate') ->action(fn (FilterPreset $record) => static::duplicatePreset($record)), ]; }
Troubleshooting
Common Issues
-
Filters are not saving correctly
- Verify that you have run the migrations
- Check that you have write permissions to the database
- Make sure filters don't contain too large data
-
Buttons don't appear
- Verify that you have added the
HasFilterPresets
trait - Check that you included
...static::getFilterPresetActions()
in table actions - Clear Laravel cache:
php artisan cache:clear
- Verify that you have added the
-
Error loading default filters
- Verify that the default filter exists
- Check that filter fields still exist in the model
- Clear config cache:
php artisan config:clear
Solutions
# Regenerate composer autoload composer dump-autoload # Clear all caches php artisan optimize:clear # Verify database integrity php artisan migrate:status # If needed, refresh migrations (CAUTION: this will delete data!) php artisan migrate:fresh
Contributing
If you want to contribute to the development of this package, please:
- Fork the repository
- Create a branch for your feature (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
This package is licensed under the MIT license.
Support
If you discover any issues or have questions, please create an issue on GitHub.
Made with ❤️ for the Filament community
☕ Buy me a coffee
If you like this project and it has been useful to you, you can buy me a coffee! ☕