Skip to content

Commit

Permalink
Merge pull request #6 from YDoomA/master
Browse files Browse the repository at this point in the history
Add soft deletes compatibility
  • Loading branch information
overtrue committed Nov 12, 2019
2 parents 2d3a799 + 9a6230d commit fa9158e
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Versionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ trait Versionable
public static function bootVersionable()
{
static::saved(function (Model $model) {
if ($model->shouldVersioning()) {
Version::createForModel($model);
$model->removeOldVersions($model->getKeepVersionsCount());
}
self::createVersionForModel($model);
});

static::deleted(function (Model $model) {
$model->removeAllVersions();
if ($model->forceDeleting) {
$model->removeAllVersions();
} else {
self::createVersionForModel($model);
}
});
}

private static function createVersionForModel(Model $model): void
{
if ($model->shouldVersioning()) {
Version::createForModel($model);
$model->removeOldVersions($model->getKeepVersionsCount());
}
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
Expand Down

0 comments on commit fa9158e

Please sign in to comment.