From 3d45452276ea3420814a2f651cd963f151b8d373 Mon Sep 17 00:00:00 2001 From: Roberto Arruda Date: Thu, 6 Aug 2020 11:06:40 -0300 Subject: [PATCH 1/3] BaseRepository::forceDelete() --- README.md | 7 +++++ .../Contracts/RepositoryInterface.php | 9 ++++++ .../Repository/Eloquent/BaseRepository.php | 29 +++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/README.md b/README.md index 5a93550b..f7e26418 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositorySe - update(array $attributes, $id) - updateOrCreate(array $attributes, array $values = []) - delete($id) +- forceDelete($id) - deleteWhere(array $where) - orderBy($column, $direction = 'asc'); - with(array $relations); @@ -431,6 +432,12 @@ Delete entry in Repository $this->repository->delete($id) ``` +Force delete entry in Repository + +```php +$this->repository->forceDelete($id) +``` + Delete entry in Repository by multiple fields ```php diff --git a/src/Prettus/Repository/Contracts/RepositoryInterface.php b/src/Prettus/Repository/Contracts/RepositoryInterface.php index 967d9f67..3da860c3 100644 --- a/src/Prettus/Repository/Contracts/RepositoryInterface.php +++ b/src/Prettus/Repository/Contracts/RepositoryInterface.php @@ -183,6 +183,15 @@ public function updateOrCreate(array $attributes, array $values = []); */ public function delete($id); + /** + * Force a hard delete on a soft deleted model to a entity in repository by id + * + * @param $id + * + * @return int + */ + public function forceDelete($id); + /** * Order collection by a given column * diff --git a/src/Prettus/Repository/Eloquent/BaseRepository.php b/src/Prettus/Repository/Eloquent/BaseRepository.php index 8cc548f6..576bb9d8 100644 --- a/src/Prettus/Repository/Eloquent/BaseRepository.php +++ b/src/Prettus/Repository/Eloquent/BaseRepository.php @@ -753,6 +753,35 @@ public function delete($id) return $deleted; } + /** + * Force a hard delete on a soft deleted model to a entity in repository by id + * + * @param $id + * + * @return int + */ + public function forceDelete($id) + { + $this->applyScope(); + + $temporarySkipPresenter = $this->skipPresenter; + $this->skipPresenter(true); + + $model = $this->find($id); + $originalModel = clone $model; + + $this->skipPresenter($temporarySkipPresenter); + $this->resetModel(); + + event(new RepositoryEntityDeleting($this, $model)); + + $deleted = $model->forceDelete(); + + event(new RepositoryEntityDeleted($this, $originalModel)); + + return $deleted; + } + /** * Delete multiple entities by given criteria. * From b090348c53abc8195b07afa3528ee4fc2efd018f Mon Sep 17 00:00:00 2001 From: Roberto Arruda Date: Fri, 25 Sep 2020 14:06:26 -0300 Subject: [PATCH 2/3] Adding events to ForceDelete --- .../Repository/Eloquent/BaseRepository.php | 6 ++++-- .../Events/RepositoryEntityForceDeleted.php | 16 ++++++++++++++++ .../Events/RepositoryEntityForceDeleting.php | 16 ++++++++++++++++ .../Providers/EventServiceProvider.php | 12 ++++++++---- 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/Prettus/Repository/Events/RepositoryEntityForceDeleted.php create mode 100644 src/Prettus/Repository/Events/RepositoryEntityForceDeleting.php diff --git a/src/Prettus/Repository/Eloquent/BaseRepository.php b/src/Prettus/Repository/Eloquent/BaseRepository.php index 576bb9d8..d4966b65 100644 --- a/src/Prettus/Repository/Eloquent/BaseRepository.php +++ b/src/Prettus/Repository/Eloquent/BaseRepository.php @@ -17,6 +17,8 @@ use Prettus\Repository\Events\RepositoryEntityCreating; use Prettus\Repository\Events\RepositoryEntityDeleted; use Prettus\Repository\Events\RepositoryEntityDeleting; +use Prettus\Repository\Events\RepositoryEntityForceDeleted; +use Prettus\Repository\Events\RepositoryEntityForceDeleting; use Prettus\Repository\Events\RepositoryEntityUpdated; use Prettus\Repository\Events\RepositoryEntityUpdating; use Prettus\Repository\Exceptions\RepositoryException; @@ -773,11 +775,11 @@ public function forceDelete($id) $this->skipPresenter($temporarySkipPresenter); $this->resetModel(); - event(new RepositoryEntityDeleting($this, $model)); + event(new RepositoryEntityForceDeleting($this, $model)); $deleted = $model->forceDelete(); - event(new RepositoryEntityDeleted($this, $originalModel)); + event(new RepositoryEntityForceDeleted($this, $originalModel)); return $deleted; } diff --git a/src/Prettus/Repository/Events/RepositoryEntityForceDeleted.php b/src/Prettus/Repository/Events/RepositoryEntityForceDeleted.php new file mode 100644 index 00000000..fac767e8 --- /dev/null +++ b/src/Prettus/Repository/Events/RepositoryEntityForceDeleted.php @@ -0,0 +1,16 @@ + + */ +class RepositoryEntityForceDeleted extends RepositoryEventBase +{ + /** + * @var string + */ + protected $action = "forceDeleted"; +} diff --git a/src/Prettus/Repository/Events/RepositoryEntityForceDeleting.php b/src/Prettus/Repository/Events/RepositoryEntityForceDeleting.php new file mode 100644 index 00000000..09a45646 --- /dev/null +++ b/src/Prettus/Repository/Events/RepositoryEntityForceDeleting.php @@ -0,0 +1,16 @@ + + */ +class RepositoryEntityForceDeleting extends RepositoryEventBase +{ + /** + * @var string + */ + protected $action = "forceDeleting"; +} diff --git a/src/Prettus/Repository/Providers/EventServiceProvider.php b/src/Prettus/Repository/Providers/EventServiceProvider.php index 7b76d062..caa4af66 100644 --- a/src/Prettus/Repository/Providers/EventServiceProvider.php +++ b/src/Prettus/Repository/Providers/EventServiceProvider.php @@ -1,4 +1,5 @@ [ - 'Prettus\Repository\Listeners\CleanCacheRepository' + 'Prettus\Repository\Listeners\CleanCacheRepository', ], 'Prettus\Repository\Events\RepositoryEntityUpdated' => [ - 'Prettus\Repository\Listeners\CleanCacheRepository' + 'Prettus\Repository\Listeners\CleanCacheRepository', ], 'Prettus\Repository\Events\RepositoryEntityDeleted' => [ - 'Prettus\Repository\Listeners\CleanCacheRepository' - ] + 'Prettus\Repository\Listeners\CleanCacheRepository', + ], + 'Prettus\Repository\Events\RepositoryEntityForceDeleted' => [ + 'Prettus\Repository\Listeners\CleanCacheRepository', + ], ]; /** From 356591f79288cbe9694dd0a5d46a31e942e1e9fb Mon Sep 17 00:00:00 2001 From: Roberto Arruda Date: Fri, 25 Sep 2020 14:46:43 -0300 Subject: [PATCH 3/3] BaseRepository::restore() --- README.md | 21 ++++++++----- .../Repository/Eloquent/BaseRepository.php | 30 +++++++++++++++++++ .../Events/RepositoryEntityRestored.php | 16 ++++++++++ .../Events/RepositoryEntityRestoring.php | 16 ++++++++++ .../Providers/EventServiceProvider.php | 3 ++ 5 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 src/Prettus/Repository/Events/RepositoryEntityRestored.php create mode 100644 src/Prettus/Repository/Events/RepositoryEntityRestoring.php diff --git a/README.md b/README.md index f7e26418..56447b0f 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,9 @@ php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositorySe - update(array $attributes, $id) - updateOrCreate(array $attributes, array $values = []) - delete($id) -- forceDelete($id) - deleteWhere(array $where) +- forceDelete($id) +- restore($id) - orderBy($column, $direction = 'asc'); - with(array $relations); - has(string $relation); @@ -432,12 +433,6 @@ Delete entry in Repository $this->repository->delete($id) ``` -Force delete entry in Repository - -```php -$this->repository->forceDelete($id) -``` - Delete entry in Repository by multiple fields ```php @@ -448,6 +443,18 @@ $this->repository->deleteWhere([ ]) ``` +Force delete entry in Repository + +```php +$this->repository->forceDelete($id) +``` + +Restore entry in Repository + +```php +$this->repository->restore($id) +``` + ### Create a Criteria #### Using the command diff --git a/src/Prettus/Repository/Eloquent/BaseRepository.php b/src/Prettus/Repository/Eloquent/BaseRepository.php index d4966b65..f62324c9 100644 --- a/src/Prettus/Repository/Eloquent/BaseRepository.php +++ b/src/Prettus/Repository/Eloquent/BaseRepository.php @@ -19,6 +19,8 @@ use Prettus\Repository\Events\RepositoryEntityDeleting; use Prettus\Repository\Events\RepositoryEntityForceDeleted; use Prettus\Repository\Events\RepositoryEntityForceDeleting; +use Prettus\Repository\Events\RepositoryEntityRestored; +use Prettus\Repository\Events\RepositoryEntityRestoring; use Prettus\Repository\Events\RepositoryEntityUpdated; use Prettus\Repository\Events\RepositoryEntityUpdating; use Prettus\Repository\Exceptions\RepositoryException; @@ -784,6 +786,34 @@ public function forceDelete($id) return $deleted; } + /** + * Restore a deleted entity in a soft deleted model in the repository by id + * + * @param $id + * + * @return int + */ + public function restore($id) + { + $this->applyScope(); + + $temporarySkipPresenter = $this->skipPresenter; + $this->skipPresenter(true); + + $model = $this->model->withTrashed()->findOrFail($id); + + $this->skipPresenter($temporarySkipPresenter); + $this->resetModel(); + + event(new RepositoryEntityRestoring($this, $model)); + + $restored = $model->restore(); + + event(new RepositoryEntityRestored($this, $model)); + + return $restored; + } + /** * Delete multiple entities by given criteria. * diff --git a/src/Prettus/Repository/Events/RepositoryEntityRestored.php b/src/Prettus/Repository/Events/RepositoryEntityRestored.php new file mode 100644 index 00000000..e12ff7ed --- /dev/null +++ b/src/Prettus/Repository/Events/RepositoryEntityRestored.php @@ -0,0 +1,16 @@ + + */ +class RepositoryEntityRestored extends RepositoryEventBase +{ + /** + * @var string + */ + protected $action = "restored"; +} diff --git a/src/Prettus/Repository/Events/RepositoryEntityRestoring.php b/src/Prettus/Repository/Events/RepositoryEntityRestoring.php new file mode 100644 index 00000000..5e11b9fa --- /dev/null +++ b/src/Prettus/Repository/Events/RepositoryEntityRestoring.php @@ -0,0 +1,16 @@ + + */ +class RepositoryEntityRestoring extends RepositoryEventBase +{ + /** + * @var string + */ + protected $action = "restoring"; +} diff --git a/src/Prettus/Repository/Providers/EventServiceProvider.php b/src/Prettus/Repository/Providers/EventServiceProvider.php index caa4af66..9384d64a 100644 --- a/src/Prettus/Repository/Providers/EventServiceProvider.php +++ b/src/Prettus/Repository/Providers/EventServiceProvider.php @@ -30,6 +30,9 @@ class EventServiceProvider extends ServiceProvider 'Prettus\Repository\Events\RepositoryEntityForceDeleted' => [ 'Prettus\Repository\Listeners\CleanCacheRepository', ], + 'Prettus\Repository\Events\RepositoryEntityRestored' => [ + 'Prettus\Repository\Listeners\CleanCacheRepository', + ], ]; /**