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 all commits
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositorySe
- updateOrCreate(array $attributes, array $values = [])
- delete($id)
- deleteWhere(array $where)
- forceDelete($id)
- restore($id)
- orderBy($column, $direction = 'asc');
- with(array $relations);
- has(string $relation);
Expand Down Expand Up @@ -441,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
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
61 changes: 61 additions & 0 deletions src/Prettus/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
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\RepositoryEntityRestored;
use Prettus\Repository\Events\RepositoryEntityRestoring;
use Prettus\Repository\Events\RepositoryEntityUpdated;
use Prettus\Repository\Events\RepositoryEntityUpdating;
use Prettus\Repository\Exceptions\RepositoryException;
Expand Down Expand Up @@ -753,6 +757,63 @@ 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 RepositoryEntityForceDeleting($this, $model));

$deleted = $model->forceDelete();

event(new RepositoryEntityForceDeleted($this, $originalModel));

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.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Prettus/Repository/Events/RepositoryEntityForceDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Prettus\Repository\Events;

/**
* Class RepositoryEntityForceDeleted
* @package Prettus\Repository\Events
* @author Roberto Arruda <robertoadearruda@gmail.com>
*/
class RepositoryEntityForceDeleted extends RepositoryEventBase
{
/**
* @var string
*/
protected $action = "forceDeleted";
}
16 changes: 16 additions & 0 deletions src/Prettus/Repository/Events/RepositoryEntityForceDeleting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Prettus\Repository\Events;

/**
* Class RepositoryEntityForceDeleting
* @package Prettus\Repository\Events
* @author Roberto Arruda <robertoadearruda@gmail.com>
*/
class RepositoryEntityForceDeleting extends RepositoryEventBase
{
/**
* @var string
*/
protected $action = "forceDeleting";
}
16 changes: 16 additions & 0 deletions src/Prettus/Repository/Events/RepositoryEntityRestored.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Prettus\Repository\Events;

/**
* Class RepositoryEntityRestored
* @package Prettus\Repository\Events
* @author Roberto Arruda <robertoadearruda@gmail.com>
*/
class RepositoryEntityRestored extends RepositoryEventBase
{
/**
* @var string
*/
protected $action = "restored";
}
16 changes: 16 additions & 0 deletions src/Prettus/Repository/Events/RepositoryEntityRestoring.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Prettus\Repository\Events;

/**
* Class RepositoryEntityRestoring
* @package Prettus\Repository\Events
* @author Roberto Arruda <robertoadearruda@gmail.com>
*/
class RepositoryEntityRestoring extends RepositoryEventBase
{
/**
* @var string
*/
protected $action = "restoring";
}
15 changes: 11 additions & 4 deletions src/Prettus/Repository/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Prettus\Repository\Providers;

use Illuminate\Support\ServiceProvider;
Expand All @@ -18,14 +19,20 @@ class EventServiceProvider extends ServiceProvider
*/
protected $listen = [
'Prettus\Repository\Events\RepositoryEntityCreated' => [
'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',
],
'Prettus\Repository\Events\RepositoryEntityRestored' => [
'Prettus\Repository\Listeners\CleanCacheRepository',
],
];

/**
Expand Down