Skip to content

Commit 3d63158

Browse files
committed
eager loading fix
1 parent 7853cfa commit 3d63158

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Traits/MorphOneWallet.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ public function wallet(): MorphOne
4242
})
4343
;
4444
}
45+
46+
public function getWalletAttribute(): ?WalletModel
47+
{
48+
$wallet = $this->getRelationValue('wallet');
49+
if ($wallet === null) {
50+
return null;
51+
}
52+
53+
assert($wallet instanceof WalletModel);
54+
55+
if (!$wallet->relationLoaded('holder')) {
56+
$holder = app(CastServiceInterface::class)->getHolder($this);
57+
$wallet->setRelation('holder', $holder);
58+
}
59+
60+
return $wallet;
61+
}
4562
}

tests/Units/Domain/EagerLoadingTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public function testUuidDuplicate(): void
3535
$balances = [];
3636
foreach ($buyers as $buyer) {
3737
self::assertTrue($buyer->relationLoaded('wallet'));
38-
// self::assertTrue($buyer->wallet->relationLoaded('holder'));
39-
// fixme: I did not find a way to load the buyer, maybe someday I will get there.
38+
self::assertTrue($buyer->wallet->relationLoaded('holder'));
4039

4140
$uuids[] = $buyer->wallet->uuid;
4241
$balances[] = $buyer->wallet->balanceInt;

0 commit comments

Comments
 (0)