Redirect from Create/Edit Page to Table with Filters

2024-11-06

In Filament tables, you may have filters, but after the user visits Create/Edit form, those filters are gone if they go back. You want to keep them active? Let's see how to enable it.


First, let's redirect the user to the list page after a record is created. This is done using the getRedirectUrl() method on the create page class.

app/Filament/Resources/ProductResource/Pages/CreateProduct.php:

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}

To keep filters active after returning to the page, we only need to add the persistFiltersInSession() method to the table.

app/Filament/Resources/ProductResource.php:

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('title'),
Tables\Columns\TextColumn::make('price')
->money(),
Tables\Columns\TextColumn::make('published_at')
->dateTime(),
])
->filters([
TernaryFilter::make('published_at')
->label('Published')
->queries(
true: fn (Builder $query) => $query->where('published_at', '<=', now()),
false: fn (Builder $query) => $query->where('published_at', '>=', now()),
blank: fn (Builder $query) => $query,
)
])
->persistFiltersInSession()
->defaultSort('created_at', 'DESC')
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

And that's it. When the user returns to the list page from any page, the filters are kept active.

A few of our Premium Examples: