33namespace Oddvalue \LaravelDrafts \Concerns ;
44
55use Illuminate \Contracts \Database \Query \Builder ;
6+ use Illuminate \Database \Eloquent \Model ;
67use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
78use Illuminate \Database \Eloquent \Relations \HasMany ;
89use Illuminate \Database \Eloquent \Relations \HasOne ;
1314use JetBrains \PhpStorm \ArrayShape ;
1415use Oddvalue \LaravelDrafts \Facades \LaravelDrafts ;
1516
17+ /**
18+ * @method void Current(Builder $query)
19+ * @method void WithoutCurrent(Builder $query)
20+ * @method void ExcludeRevision(Builder $query, int | Model $exclude)
21+ */
1622trait HasDrafts
1723{
1824 use Publishes;
@@ -86,7 +92,7 @@ protected function newRevision(): void
8692 return ;
8793 }
8894
89- $ revision = $ this ->fresh ()->replicate ();
95+ $ revision = $ this ->fresh ()? ->replicate();
9096
9197 static ::saved (function () use ($ revision ) {
9298 $ revision ->created_at = $ this ->created_at ;
@@ -118,7 +124,7 @@ public function generateUuid(): void
118124
119125 public function setCurrent (): void
120126 {
121- $ oldCurrent = $ this ->revisions ()->withDrafts ()->current ()->withoutSelf ( )->first ();
127+ $ oldCurrent = $ this ->revisions ()->withDrafts ()->current ()->excludeRevision ( $ this )->first ();
122128
123129 static ::saved (function () use ($ oldCurrent ) {
124130 if ($ oldCurrent ) {
@@ -133,7 +139,7 @@ public function setCurrent(): void
133139
134140 public function setLive (): void
135141 {
136- $ published = $ this ->revisions ()->withoutSelf ( )->published ()->first ();
142+ $ published = $ this ->revisions ()->excludeRevision ( $ this )->published ()->first ();
137143
138144 if (! $ published ) {
139145 $ this ->{$ this ->getPublishedAtColumn ()} ??= now ();
@@ -278,7 +284,7 @@ public function setPublisher(): static
278284
279285 public function pruneRevisions ()
280286 {
281- $ this -> withoutEvents (function () {
287+ self :: withoutEvents (function () {
282288 $ revisionsToKeep = $ this ->revisions ()
283289 ->orderByDesc ('updated_at ' )
284290 ->onlyDrafts ()
@@ -374,7 +380,15 @@ public function scopeWithoutCurrent(Builder $query): void
374380 $ query ->where ($ this ->getIsCurrentColumn (), false );
375381 }
376382
377- public function scopeWithoutSelf (Builder $ query )
383+ public function scopeExcludeRevision (Builder $ query , int | Model $ exclude ): void
384+ {
385+ $ query ->where ($ this ->getKeyName (), '!= ' , is_int ($ exclude ) ? $ exclude : $ exclude ->getKey ());
386+ }
387+
388+ /**
389+ * @deprecated This doesn't actually work, will be removed in next version
390+ */
391+ public function scopeWithoutSelf (Builder $ query ): void
378392 {
379393 $ query ->where ('id ' , '!= ' , $ this ->id );
380394 }
0 commit comments