diff --git a/src/Traits/HasWallets.php b/src/Traits/HasWallets.php index 3cedfe814..fc0f519da 100644 --- a/src/Traits/HasWallets.php +++ b/src/Traits/HasWallets.php @@ -62,14 +62,14 @@ public function getWalletOrFail(string $slug): WalletModel if ($this->_wallets === [] && $this->relationLoaded('wallets')) { /** @var WalletModel $wallet */ foreach ($this->getRelation('wallets') as $wallet) { - $wallet->setRelation('holder', $this); + $wallet->setRelation('holder', $this->withoutRelations()); $this->_wallets[$wallet->slug] = $wallet; } } if (!array_key_exists($slug, $this->_wallets)) { $wallet = app(WalletServiceInterface::class)->getBySlug($this, $slug); - $wallet->setRelation('holder', $this); + $wallet->setRelation('holder', $this->withoutRelations()); $this->_wallets[$slug] = $wallet; } @@ -89,7 +89,7 @@ public function createWallet(array $data): WalletModel { $wallet = app(WalletServiceInterface::class)->create($this, $data); $this->_wallets[$wallet->slug] = $wallet; - $wallet->setRelation('holder', $this); + $wallet->setRelation('holder', $this->withoutRelations()); return $wallet; } diff --git a/src/Traits/MorphOneWallet.php b/src/Traits/MorphOneWallet.php index c10d94c75..cbf76e809 100644 --- a/src/Traits/MorphOneWallet.php +++ b/src/Traits/MorphOneWallet.php @@ -34,7 +34,12 @@ public function wallet(): MorphOne ])); if (property_exists($holder, 'exists') && $holder->exists) { - $wallet->setRelation('holder', $holder); + $wallet->setRelation( + 'holder', + method_exists($holder, 'withoutRelations') + ? $holder->withoutRelations() + : $holder + ); } }) ; diff --git a/tests/Units/Domain/EagerLoadingTest.php b/tests/Units/Domain/EagerLoadingTest.php index 5a6daa0f6..51985a517 100644 --- a/tests/Units/Domain/EagerLoadingTest.php +++ b/tests/Units/Domain/EagerLoadingTest.php @@ -62,10 +62,8 @@ public function testTransferTransactions(): void self::assertTrue($transfer->relationLoaded('from')); self::assertTrue($transfer->relationLoaded('to')); - self::assertSame($user1->wallet->getMorphClass(), $transfer->from->getMorphClass()); - self::assertSame($user1->wallet->getKey(), $transfer->from->getKey()); - self::assertSame($user2->wallet->getMorphClass(), $transfer->to->getMorphClass()); - self::assertSame($user2->wallet->getKey(), $transfer->to->getKey()); + self::assertTrue($user1->wallet->is($transfer->from)); + self::assertTrue($user2->wallet->is($transfer->to)); } public function testMultiWallets(): void @@ -86,6 +84,6 @@ public function testMultiWallets(): void self::assertNotNull($user->getWallet('hello')); self::assertNotNull($user->getWallet('world')); self::assertTrue($user->getWallet('hello')->relationLoaded('holder')); - self::assertSame($user, $user->getWallet('hello')->holder); + self::assertTrue($user->is($user->getWallet('hello')->holder)); } }