Tricks

Add SoftDeletes support, to existing Resource

Sep 30, 2022
Tina Hammar
Admin panel

When generating a new Resource

The documentation says to use the --soft-deletes flag when generating the resource:

php artisan make:filament-resource Customer --soft-deletes

But how do you add "softDelete" features to an existing Resource?

Import the scope:

use Illuminate\Database\Eloquent\SoftDeletingScope;

Add Table filters and actions related to soft deletes:

public static function table(Table $table): Table
{
return $table
->columns([
// ...
])
->filters([
Tables\Filters\TrashedFilter::make(),
])
->actions([
// ...
])
->bulkActions([
Tables\Actions\RestoreBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
]);
}

Add this method to the resource. It modifies the query to include soft deleted records:

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}

No comments yet…