diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index ffca6c6..c321b0f 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -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(); + }); + } } }