Skip to content

Commit ccc7b25

Browse files
committed
isOwn fix
1 parent 869107b commit ccc7b25

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/Interfaces/Ownable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public function own(?Owner $owner = null): static;
1818

1919
public function unown(Owner $owner): static;
2020

21+
public function isOwn(?Owner $owner = null): bool;
22+
2123
public function attachOwner(?Owner $owner = null): static;
2224

2325
public function attachOwners(?Collection $owners = null): static;

src/Observers/OwnableObserver.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ public function created(Ownable $ownable): void
1111
{
1212
$options = $ownable->ownable();
1313

14-
if ($options->auto) {
15-
$ownable->attachOwner();
14+
if (! $options->auto) {
15+
return;
1616
}
17+
18+
if ($ownable->isOwn()) {
19+
return;
20+
}
21+
22+
$ownable->attachOwner();
1723
}
1824

1925
public function deleted(Ownable $ownable): void

src/Traits/HasOwners.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ public function unown(Owner $owner): static
4343
return $this->detachOwner($owner);
4444
}
4545

46-
public function isOwn(Owner $owner): bool
46+
public function isOwn(?Owner $owner = null): bool
4747
{
4848
$pivot = new Ownership();
4949
$relation = $pivot->owner();
50+
$owner ??= $this->defaultOwner();
5051

5152
$attributes = [
5253
$relation->getForeignKeyName() => $owner instanceof Model ? $owner->getKey() : null,

0 commit comments

Comments
 (0)