From dce507d18cd6de608b9bfb1c05837e13d2ddaf20 Mon Sep 17 00:00:00 2001 From: overtrue Date: Sat, 2 Apr 2022 11:34:16 +0800 Subject: [PATCH] Fixed #29 --- src/Versionable.php | 7 ++++++- tests/FeatureTest.php | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Versionable.php b/src/Versionable.php index ec7c3fb..e4eb785 100644 --- a/src/Versionable.php +++ b/src/Versionable.php @@ -50,7 +50,7 @@ private static function createVersionForModel(Model $model): void */ public function versions(): MorphMany { - return $this->morphMany(\config('versionable.version_model'), 'versionable')->latest('id'); + return $this->morphMany(\config('versionable.version_model'), 'versionable'); } /** @@ -69,6 +69,11 @@ public function latestVersion(): MorphOne return $this->morphOne(\config('versionable.version_model'), 'versionable')->latest('id'); } + public function firstVersion(): ?Model + { + return $this->versions()->oldest('id')->first(); + } + /** * @param int $id * diff --git a/tests/FeatureTest.php b/tests/FeatureTest.php index 54e110b..f414c5d 100644 --- a/tests/FeatureTest.php +++ b/tests/FeatureTest.php @@ -72,6 +72,12 @@ public function post_can_revert_to_target_version() $post->update(['title' => 'version3', 'content' => 'version3 content', 'extends' => ['name' => 'overtrue']]); $post->update(['title' => 'version4', 'content' => 'version4 content']); + // #29 + $version = $post->firstVersion(); + $post = $version->revertWithoutSaving(); + $this->assertSame('version1', $post->title); + $this->assertSame('version1 content', $post->content); + // revert version 2 $post->revertToVersion(2); $post->refresh();