Skip to content

Commit

Permalink
eager loading fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Apr 26, 2022
1 parent 7853cfa commit 3d63158
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/Traits/MorphOneWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,21 @@ public function wallet(): MorphOne
})
;
}

public function getWalletAttribute(): ?WalletModel
{
$wallet = $this->getRelationValue('wallet');
if ($wallet === null) {
return null;
}

assert($wallet instanceof WalletModel);

if (!$wallet->relationLoaded('holder')) {
$holder = app(CastServiceInterface::class)->getHolder($this);
$wallet->setRelation('holder', $holder);
}

return $wallet;
}
}
3 changes: 1 addition & 2 deletions tests/Units/Domain/EagerLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public function testUuidDuplicate(): void
$balances = [];
foreach ($buyers as $buyer) {
self::assertTrue($buyer->relationLoaded('wallet'));
// self::assertTrue($buyer->wallet->relationLoaded('holder'));
// fixme: I did not find a way to load the buyer, maybe someday I will get there.
self::assertTrue($buyer->wallet->relationLoaded('holder'));

$uuids[] = $buyer->wallet->uuid;
$balances[] = $buyer->wallet->balanceInt;
Expand Down

0 comments on commit 3d63158

Please sign in to comment.