Skip to content

Commit

Permalink
Fixed #29
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 2, 2022
1 parent e0afe3f commit dce507d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Versionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -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
*
Expand Down
6 changes: 6 additions & 0 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit dce507d

Please sign in to comment.