| Install | |
|---|---|
composer require martinpetricko/filament-restore-or-create |
|
| Latest Version: | 2.0.0 |
| PHP: | ^8.2 |
Restore or Create is a FilamentPHP plugin that helps prevent duplicate records by detecting and restoring soft-deleted models when similar data is submitted via a create form.
Install via Composer:
composer require martinpetricko/filament-restore-or-create
Optionally, publish the translation files:
php artisan vendor:publish --tag="filament-restore-or-create-translations"
Add CheckDeleted trait to your resource's CreateRecord page:
use MartinPetricko\FilamentRestoreOrCreate\Concerns\CreateRecord\CheckDeleted;
class CreateUser extends CreateRecord
{
use CheckDeleted;
protected static string $resource = UserResource::class;
}
Define which fields should be used to detect similar deleted records:
protected function checkDeletedAttributes(): array
{
return ['name', 'email', 'phone'];
}
Override the default query to define your own matching logic:
protected function checkDeletedModel(array $data): ?Model
{
return static::getResource()::getEloquentQuery()
->whereLike('name', '%' . $data['name'] . '%')
->onlyTrashed()
->latest()
->first();
}
Choose which attributes to show in the confirmation modal:
protected function showDeletedAttributes(): ?array
{
return ['name', 'email', 'phone', 'address', 'deleted_at'];
}
Customize the notification shown after a record is restored:
protected function getRestoredNotification(): ?Notification
{
return Notification::make()
->success()
->title('Restored');
}
Control where the user is redirected after restoring:
protected function getRestoreRedirectUrl(Model $record): string
{
/** @var class-string<Resource> $resource */
$resource = static::getResource();
if ($resource::hasPage('edit') && $resource::canEdit($record)) {
return $resource::getUrl('edit', ['record' => $record]);
}
return $resource::getUrl();
}
If you override beforeCreate, ensure the restore behavior is still called:
class CreateUser extends CreateRecord
{
use CheckDeleted {
beforeCreate as checkDeletedBeforeCreate;
}
protected function beforeCreate(): void
{
$this->checkDeletedBeforeCreate();
// Your custom logic
}
}
composer test
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.