Skip to content

Persist Table Filters per Tenant in Filament

Blog  ✺  Filament  ✺  Code Snippet

Filament allows you to persist table filters, sorts and searches in the user session. In a multi-tenant app, this might not be the desired behavior, as filters could be different and search might return no results.

Internally, Filament creates the session keys based on the class name:

public function getTableFiltersSessionKey(): string
{
    $table = md5($this::class);

    return "tables.{$table}_filters";
}

To change this, we can create a simple trait and add it to our resource pages that are extending ListRecords (as an example).


Filters, Searches and Sorts per Tenant

In the code below, we overwrite the way Filament creates the session keys for table sort, filter and searches.

How to use it?

For multi-tenant apps using Filament, you can use the code if you are persisting the table searches, filters and sorts in the session.

Example Resource

<?php

namespace App\Filament\Resources;

class ExampleResource extends Resource
{
    public static function table(Table $table): Table
    {
        return $table
            ->persistFiltersInSession()
            ->persistSortInSession()
            ->persistSearchInSession();
    }
}

Example List Records page

<?php

namespace App\Filament\Resources\ProductResource\Pages;

use App\Filament\Traits\HasCatalogAwareSessionKeys;

class ListExamples extends ListRecords
{
    use HasCatalogAwareSessionKeys;
}

That's it, now the filters, search and sorts for your tables are stored individually per tenant.


Before you go, let me know on twitter what type of tutorials you would like to watch and read? Stay in touch? Sign up to my newsletter.

Comments

You might like

Laracon EU 2025 - My Recap

Laravel  ✺  Opinion

Add Dimensions to Image and Video Uploads in Laravel

Laravel