Skip to content

Commit

Permalink
Merge pull request #8 from spatie/review
Browse files Browse the repository at this point in the history
Review
  • Loading branch information
freekmurze authored Jan 16, 2023
2 parents 4898e19 + c824daa commit 2a64f82
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions database/migrations/create_deleted_models_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function up()
$table->json('values');

$table->timestamps();

$table->unique(['model', 'key']);
});
}
};
Empty file removed resources/views/.gitkeep
Empty file.
17 changes: 11 additions & 6 deletions src/Models/DeletedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,8 @@ class DeletedModel extends Model

public function restore(Closure $beforeSaving = null): Model
{
DB::beginTransaction();

event(new RestoringDeletedModelEvent($this));

try {
Expand All @@ -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;
}
Expand Down

0 comments on commit 2a64f82

Please sign in to comment.