Cascade soft delete and restore.
Created
April 28, 2016 16:58
-
-
Save jesseleite/e587f7bd21a942d36cf49d642e0aa7bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'user' => [ | |
'sessions', | |
], | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\DB\Listeners; | |
class Cascade | |
{ | |
/** | |
* Trigger cascade function on Eloquent Model if defined in softdeletes config. | |
* @param object $model Eloquent Model | |
* @param string $direction restore|delete | |
* @return mixed | |
*/ | |
public function cascade($model, $direction) | |
{ | |
$setting = sprintf('softdeletes.%s', strtolower((new \ReflectionClass($model))->getShortName())); | |
if ($options = config($setting)) { | |
array_map(function ($tbl) use ($model, $direction) { | |
$model->$tbl()->$direction(); | |
}, $options); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\DB\Listeners; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
class CascadeDeleteListener extends Cascade | |
{ | |
/** | |
* Handel the event for eloquent delete. | |
* @param $model | |
* @return void | |
*/ | |
public function handle($model) | |
{ | |
/** TO-DO: find a way to check if we called delete or forceDelete because this invalidates tests. */ | |
$this->cascade($model, app()->environment('local') ? 'forceDelete' : 'delete'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\DB\Listeners; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
class CascadeRestoreListener extends Cascade | |
{ | |
/** | |
* Handel the event for eloquent restore. | |
* @param $model | |
* @return void | |
*/ | |
public function handle($model) | |
{ | |
$this->cascade($model, 'restore'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\DB\Providers; | |
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; | |
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; | |
class EventServiceProvider extends ServiceProvider | |
{ | |
/** | |
* The event listener mappings for the application. | |
* | |
* @var array | |
*/ | |
protected $listen = [ | |
'eloquent.deleting: *' => ['App\DB\Listeners\CascadeDeleteListener'], | |
'eloquent.restoring: *' => ['App\DB\Listeners\CascadeRestoreListener'], | |
]; | |
/** | |
* Register any other events for your application. | |
* | |
* @param \Illuminate\Contracts\Events\Dispatcher $events | |
* @return void | |
*/ | |
public function boot(DispatcherContract $events) | |
{ | |
parent::boot($events); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment