kisame76/filament-db-table-state
Persist Filament table state (filters, sorting, search, pagination) in the database so users keep their preferred view between visits. Lightweight Laravel package for per-user table preferences across resources and sessions.
Installation
composer require kisame76/filament-db-table-state
Publish the config (if needed):
php artisan vendor:publish --provider="Kisame76\FilamentDbTableState\FilamentDbTableStateServiceProvider" --tag="config"
Basic Usage Add the trait to your Filament table class:
use Kisame76\FilamentDbTableState\Concerns\HasDbTableState;
class MyTable extends Filament\Tables\Table
{
use HasDbTableState;
// Table configuration...
}
The package now persists table state (sorting, filtering, pagination) per user in the database.
First Use Case Enable state persistence for a table:
protected static ?string $stateful = true;
The table will now remember user preferences across sessions.
State Management
auth()->id().$this->setStateUserId(123); // Force a specific user ID
Conditional State Disable state for specific actions (e.g., bulk actions):
public function getStateful(): bool
{
return ! request()->has('bulk_action');
}
Custom State Keys Use a custom table name or key for isolation:
protected static ?string $stateKey = 'custom_table_state';
State Migration Migrate existing state from session to DB (if switching from Filament’s default):
php artisan filament-db-table-state:migrate
SoftDeletes models.StateRepository to scope by tenant:
$this->stateRepository()->setTenantId(auth()->tenant()->id);
State Key Collisions
filament_tables_state_{table_class}.$stateKey to avoid conflicts in multi-table apps.Performance with Large Datasets
->withoutGlobalScopes() for complex state.Testing Quirks
StateRepository in tests:
$this->partialMock(StateRepository::class, fn ($mock) => $mock->shouldReceive('getState')->andReturn([]));
Database Schema
filament_table_states table.\Log::debug('Current state:', $this->getState());
$this->clearState();
Or via CLI:
php artisan filament-db-table-state:clear
Custom State Storage
Bind your own StateRepository:
$this->app->bind(
StateRepository::class,
fn () => new CustomStateRepository()
);
State Validation
Override validateState() to sanitize input:
protected function validateState(array $state): void
{
$state['perPage'] = min(max((int) $state['perPage'] ?? 10, 1), 100);
}
Event Hooks Listen for state updates:
event(new TableStateUpdated($table, $state));
How can I help you explore Laravel packages today?