diff --git a/README.md b/README.md index bfb1025..5859241 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,12 @@ $revisions = $post->revisions(); Deleting a record will also delete all of its revisions. Soft deleting records will soft delete the revisions and restoring records will restore the revisions. +If you need to update a record without creating revision + +```php +$post->withoutRevision()->update($options); +``` + ## Testing ```bash diff --git a/src/Concerns/HasDrafts.php b/src/Concerns/HasDrafts.php index a541821..f0662e8 100644 --- a/src/Concerns/HasDrafts.php +++ b/src/Concerns/HasDrafts.php @@ -114,6 +114,13 @@ protected function newRevision(): void }); } + public function withoutRevision(): static + { + $this->shouldCreateRevision = false; + + return $this; + } + public function shouldCreateRevision(): bool { return $this->shouldCreateRevision; diff --git a/tests/RevisionsTest.php b/tests/RevisionsTest.php index 276385f..e333855 100644 --- a/tests/RevisionsTest.php +++ b/tests/RevisionsTest.php @@ -102,3 +102,19 @@ $this->assertDatabaseCount(SoftDeletingPost::class, 6); expect(SoftDeletingPost::withDrafts()->count())->toBe(6); }); + +it('save without revision', function () { + $post = Post::factory()->published()->create(['title' => 'Foo']); + $this->assertDatabaseCount('posts', 1); + + $post->withoutRevision(); + + $post->title = 'Bar'; + $post->save(); + + $this->assertDatabaseCount('posts', 1); + + $this->assertDatabaseHas('posts', [ + 'title' => $post->title + ]); +});