gerenuk / filament-table-views
Saved table views and column-based advanced filters for Filament.
Requires
- php: ^8.4
- filament/filament: ^5.11.0
- illuminate/contracts: ^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^11.0
- pestphp/pest: ^5.0
- pestphp/pest-plugin-arch: ^5.0
- pestphp/pest-plugin-laravel: ^5.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Filament Table Views
This package lets you save table views in your Filament panel, with filters that build themselves from your table's columns. Users save the current filters, sort, search and columns, share them, pin favourites above the table and come back to them from a link.
Table of Contents
- Introduction
- Version Compatibility
- Installation
- Usage
- Testing
- Changelog
- Contributing
- Security Vulnerabilities
- Credits
- License
Version Compatibility
| Plugin | Filament | Laravel | PHP |
|---|---|---|---|
| 1.x | 5.11+ | 13.x | 8.4|8.5 |
Installation
You can install the package via composer:
composer require gerenuk/filament-table-views
Views belong to the model of your default auth provider. Their user_id columns match that model's key, whether it uses integer IDs, UUIDs or ULIDs. If your panel signs in a different model, publish the config and set user_model before publishing and running the migration:
php artisan vendor:publish --tag="filament-table-views-config"
'user_model' => App\Models\Admin::class,
Publish the migration and run it. It creates the table_views and table_view_favorites tables. The published migration can be edited before you run it.
php artisan vendor:publish --tag="filament-table-views-migrations"
php artisan migrate
Publish the package's stylesheet. Filament's filament:upgrade script does this for you if it runs after composer update:
php artisan filament:assets
Optionally, you can publish the translations using
php artisan vendor:publish --tag="filament-table-views-translations"
Usage
Registering the Plugin
Add the plugin to your panel:
use Gerenuk\FilamentTableViews\FilamentTableViewsPlugin; public function panel(Panel $panel): Panel { return $panel // ... ->plugin(FilamentTableViewsPlugin::make()); }
Column Filters
Add ColumnQueryBuilder to a table's filters. It reads the table's columns and adds a rule for each column backed by the database:
use Gerenuk\FilamentTableViews\Filters\ColumnQueryBuilder; public static function table(Table $table): Table { return $table ->columns([ TextColumn::make('reference')->searchable()->sortable(), TextColumn::make('status.name')->label('Status'), TextColumn::make('total')->money('GBP'), IconColumn::make('is_paid')->boolean(), TextColumn::make('placed_at')->dateTime(), ]) ->filters([ ColumnQueryBuilder::make(), ]); }
The type of each rule comes from the column and the database:
| Column | Rule |
|---|---|
| Toggle, checkbox, boolean icon, or a boolean cast or column type | Boolean |
date() or dateTime() text column, or a date column type |
Date |
numeric() or money() text column, or a numeric column type |
Number |
A column on a direct relationship, such as status.name |
"Is related to", a searchable select |
| Anything else | Text |
Deeper relationships, such as customer.company.name, filter on the related value. Image columns, and columns that aren't backed by the database (such as accessors), are skipped.
Leave columns out with excludeColumns(). Rules you add yourself with constraints() replace a generated rule of the same name:
ColumnQueryBuilder::make() ->excludeColumns(['internal_note']) ->constraints([ TextConstraint::make('notes'), ]),
If the table hasn't chosen a filter layout, the filters open in a slide-over wide enough to keep each rule on one line. Any layout or width the table sets itself is kept.
Saved Views
Add the HasTableViews trait to a resource's list page:
use Filament\Resources\Pages\ListRecords; use Gerenuk\FilamentTableViews\Concerns\HasTableViews; class ListOrders extends ListRecords { use HasTableViews; protected static string $resource = OrderResource::class; }
This adds a bar above the table:
- Save view saves the current filters, sort, search and columns, with a name, icon and colour. Users can share a view with everyone and add it to their favourites.
- Favourites show as tabs next to Default view. Users drag the tabs to reorder them.
- Update view appears when the open view is yours and the table has changed since it was saved. A dot on the tab shows the same thing.
- Views opens a slide-over to search, apply, favourite, edit and delete views. Only the owner of a view can edit or delete it.
Views are stored per resource, so renaming the list page keeps them.
Colours show as a row of circles, and every outline and solid Heroicon is offered (searchable). Choose your own with colors() and icons():
FilamentTableViewsPlugin::make() ->colors(['success', 'info', 'warning', 'danger', 'gray']) ->icons([Heroicon::OutlinedStar, Heroicon::OutlinedFlag]),
colors() takes Filament colour names. The default is grey, primary, blue, green, amber and red. Leave icons() out to offer every Heroicon.
Favourites for Everyone
A favourite for everyone is pinned to every user's bar, before their own favourites. Choose who can set one with globalFavorites():
FilamentTableViewsPlugin::make() ->globalFavorites(fn (User $user): bool => $user->isAdmin()),
Those users get a "Favourite for everyone" toggle when saving or editing their own views. A favourite for everyone is always shared.
User Views Page
The User Views page is off by default. Turn it on and choose who can open it:
FilamentTableViewsPlugin::make() ->userViews(fn (User $user): bool => $user->isAdmin()) ->userViewsNavigationGroup('Settings'), // optional
The page:
- lists every saved view in the current tenant, whoever owns it;
- lets those users rename, recolour, share or unshare, and delete any view, and set favourites for everyone (when they also pass
globalFavorites()); - lets them drag favourites for everyone into order on the "Favourites for everyone" tab.
Only the users that userViews() allows can open the page or use its actions.
Placing the Views Bar
The trait adds the bar through the page's content() method. If your page overrides content(), add the bar yourself:
public function content(Schema $schema): Schema { return $schema ->components([ $this->getTabsContentComponent(), $this->getTableViewsComponent(), EmbeddedTable::make(), ]); }
How Views Behave
- Links: the open view is kept in the URL as
?view=, so a reload keeps it open. A link with only?view=applies the whole view. - Changed tables: if a column, filter or rule type has been removed since a view was saved, that part of the view is dropped and the rest still loads.
- Visibility: users see their own views and views shared with everyone. In a panel with tenancy, views, including shared views and favourites for everyone, belong to the tenant they were saved in.
Testing
composer test
composer analyse
composer format
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.