Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Filament Db Table State Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require kisame76/filament-db-table-state
    

    Publish the config (if needed):

    php artisan vendor:publish --provider="Kisame76\FilamentDbTableState\FilamentDbTableStateServiceProvider" --tag="config"
    
  2. 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.

  3. First Use Case Enable state persistence for a table:

    protected static ?string $stateful = true;
    

    The table will now remember user preferences across sessions.


Implementation Patterns

Workflow Integration

  1. State Management

    • Per-User State: Automatically scoped to authenticated users via auth()->id().
    • Manual Overrides: Override the user ID for testing or shared sessions:
      $this->setStateUserId(123); // Force a specific user ID
      
  2. Conditional State Disable state for specific actions (e.g., bulk actions):

    public function getStateful(): bool
    {
        return ! request()->has('bulk_action');
    }
    
  3. Custom State Keys Use a custom table name or key for isolation:

    protected static ?string $stateKey = 'custom_table_state';
    
  4. State Migration Migrate existing state from session to DB (if switching from Filament’s default):

    php artisan filament-db-table-state:migrate
    

Advanced Patterns

  • Soft Deletes: Works seamlessly with SoftDeletes models.
  • Multi-Tenant: Extend the StateRepository to scope by tenant:
    $this->stateRepository()->setTenantId(auth()->tenant()->id);
    

Gotchas and Tips

Pitfalls

  1. State Key Collisions

    • Default key: filament_tables_state_{table_class}.
    • Fix: Use $stateKey to avoid conflicts in multi-table apps.
  2. Performance with Large Datasets

    • Heavy filtering/sorting may slow queries.
    • Tip: Cache frequent queries or use ->withoutGlobalScopes() for complex state.
  3. Testing Quirks

    • Mock the StateRepository in tests:
      $this->partialMock(StateRepository::class, fn ($mock) => $mock->shouldReceive('getState')->andReturn([]));
      
  4. Database Schema

    • The package creates a filament_table_states table.
    • Tip: Customize the migration if extending the schema.

Debugging Tips

  • Log State Data:
    \Log::debug('Current state:', $this->getState());
    
  • Clear State Manually:
    $this->clearState();
    
    Or via CLI:
    php artisan filament-db-table-state:clear
    

Extension Points

  1. Custom State Storage Bind your own StateRepository:

    $this->app->bind(
        StateRepository::class,
        fn () => new CustomStateRepository()
    );
    
  2. State Validation Override validateState() to sanitize input:

    protected function validateState(array $state): void
    {
        $state['perPage'] = min(max((int) $state['perPage'] ?? 10, 1), 100);
    }
    
  3. Event Hooks Listen for state updates:

    event(new TableStateUpdated($table, $state));
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky