From 2aac6ca920026965507415c4b4c5091f9fba5c4e Mon Sep 17 00:00:00 2001 From: Rias Date: Mon, 16 Jan 2023 16:01:26 +0100 Subject: [PATCH 1/3] Delete views directory as it's not needed --- resources/views/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 resources/views/.gitkeep diff --git a/resources/views/.gitkeep b/resources/views/.gitkeep deleted file mode 100644 index e69de29..0000000 From 9e68e1a54e1804fd26d1ce2f1bc3b9ed52a4bca3 Mon Sep 17 00:00:00 2001 From: Rias Date: Mon, 16 Jan 2023 16:01:43 +0100 Subject: [PATCH 2/3] Index on model & key --- database/migrations/create_deleted_models_table.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/database/migrations/create_deleted_models_table.php b/database/migrations/create_deleted_models_table.php index ed0c0fa..0142fd5 100644 --- a/database/migrations/create_deleted_models_table.php +++ b/database/migrations/create_deleted_models_table.php @@ -16,6 +16,8 @@ public function up() $table->json('values'); $table->timestamps(); + + $table->unique(['model', 'key']); }); } }; From c824daad881c15c1ea535bafb1f7429d16b01d65 Mon Sep 17 00:00:00 2001 From: Rias Date: Mon, 16 Jan 2023 16:02:12 +0100 Subject: [PATCH 3/3] Use a DB transaction for restore --- src/Models/DeletedModel.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Models/DeletedModel.php b/src/Models/DeletedModel.php index 1557b0b..dc4a9d0 100644 --- a/src/Models/DeletedModel.php +++ b/src/Models/DeletedModel.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\DB; use Spatie\DeletedModels\Events\DeletedModelRestoredEvent; use Spatie\DeletedModels\Events\RestoringDeletedModelEvent; use Spatie\DeletedModels\Exceptions\CouldNotRestoreModel; @@ -30,6 +31,8 @@ class DeletedModel extends Model public function restore(Closure $beforeSaving = null): Model { + DB::beginTransaction(); + event(new RestoringDeletedModelEvent($this)); try { @@ -44,15 +47,17 @@ public function restore(Closure $beforeSaving = null): Model $this->saveRestoredModel($restoredModel); $this->afterSavingRestoredModel(); - } catch (Exception $exception) { - $this->handleExceptionDuringRestore($exception); - throw $exception; - } + event(new DeletedModelRestoredEvent($this, $restoredModel)); - $this->deleteDeletedModel(); + $this->deleteDeletedModel(); - event(new DeletedModelRestoredEvent($this, $restoredModel)); + DB::commit(); + } catch (Exception $exception) { + DB::rollBack(); + + $this->handleExceptionDuringRestore($exception); + } return $restoredModel; }