Skip to content

Commit

Permalink
Merge pull request #55 from fedeisas/patch-1
Browse files Browse the repository at this point in the history
Save a COUNT query if all records should be kept
  • Loading branch information
nonoesp authored Nov 8, 2020
2 parents e21bded + ef5832a commit f3ad4b4
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/Mpociot/Versionable/VersionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,20 @@ protected function versionablePostSave()
private function purgeOldVersions()
{
$keep = isset($this->keepOldVersions) ? $this->keepOldVersions : 0;
$count = $this->versions()->count();

if ((int)$keep > 0 && $count > $keep) {
$oldVersions = $this->versions()
->latest()
->take($count)
->skip($keep)
->get()
->each(function ($version) {
$version->delete();
});

if ((int)$keep > 0) {
$count = $this->versions()->count();

if ($count > $keep) {
$oldVersions = $this->versions()
->latest()
->take($count)
->skip($keep)
->get()
->each(function ($version) {
$version->delete();
});
}
}
}

Expand Down

0 comments on commit f3ad4b4

Please sign in to comment.