Skip to content

Commit a45624f

Browse files
committed
Check we have the correct model when using events
1 parent 16fa512 commit a45624f

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/Concerns/HasDrafts.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ protected function newRevision(): void
9494

9595
$revision = $this->fresh()?->replicate();
9696

97-
static::saved(function () use ($revision) {
97+
static::saved(function (Model $model) use ($revision): void {
98+
if ($model->isNot($this)) {
99+
return;
100+
}
101+
98102
$revision->created_at = $this->created_at;
99103
$revision->updated_at = $this->updated_at;
100104
$revision->is_current = false;
@@ -126,22 +130,24 @@ public function setCurrent(): void
126130
{
127131
$oldCurrent = $this->revisions()->withDrafts()->current()->excludeRevision($this)->first();
128132

129-
static::saved(function () use ($oldCurrent) {
130-
if ($oldCurrent) {
131-
$oldCurrent->{$this->getIsCurrentColumn()} = false;
132-
$oldCurrent->timestamps = false;
133-
$oldCurrent->saveQuietly();
133+
static::saved(function (Model $model) use ($oldCurrent): void {
134+
if ($model->isNot($this) || ! $oldCurrent) {
135+
return;
134136
}
137+
138+
$oldCurrent->{$this->getIsCurrentColumn()} = false;
139+
$oldCurrent->timestamps = false;
140+
$oldCurrent->saveQuietly();
135141
});
136142

137143
$this->{$this->getIsCurrentColumn()} = true;
138144
}
139145

140146
public function setLive(): void
141147
{
142-
$published = $this->revisions()->excludeRevision($this)->published()->first();
148+
$published = $this->revisions()->published()->first();
143149

144-
if (! $published) {
150+
if (! $published || $this->is($published)) {
145151
$this->{$this->getPublishedAtColumn()} ??= now();
146152
$this->{$this->getIsPublishedColumn()} = true;
147153
$this->setCurrent();
@@ -157,7 +163,11 @@ public function setLive(): void
157163
$published->forceFill($newAttributes);
158164
$this->forceFill($oldAttributes);
159165

160-
static::saved(function () use ($published) {
166+
static::saved(function (Model $model) use ($published): void {
167+
if ($model->isNot($this)) {
168+
return;
169+
}
170+
161171
$published->{$this->getIsPublishedColumn()} = true;
162172
$published->{$this->getPublishedAtColumn()} ??= now();
163173
$published->setCurrent();

src/Concerns/Publishes.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Oddvalue\LaravelDrafts\Concerns;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Oddvalue\LaravelDrafts\Scopes\PublishingScope;
67

78
/**
@@ -48,7 +49,11 @@ public function publish(): static
4849
$this->{$this->getPublishedAtColumn()} ??= now();
4950
$this->{$this->getIsPublishedColumn()} = true;
5051

51-
static::saved(function () {
52+
static::saved(function (Model $record): void {
53+
if ($record->getKey() !== $this->getKey()) {
54+
return;
55+
}
56+
5257
$this->fireModelEvent('published');
5358
});
5459

0 commit comments

Comments
 (0)