Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseRepository::forceDelete() #722

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/Prettus/Repository/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
29 changes: 29 additions & 0 deletions src/Prettus/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
robertoarruda marked this conversation as resolved.
Show resolved Hide resolved

$deleted = $model->forceDelete();

event(new RepositoryEntityDeleted($this, $originalModel));
robertoarruda marked this conversation as resolved.
Show resolved Hide resolved

return $deleted;
}

/**
* Delete multiple entities by given criteria.
*
Expand Down