Skip to content

Commit aa034b3

Browse files
committed
fix: findById() does not work with withIdentities()
ErrorException : Attempt to read property "id" on int
1 parent d2aff61 commit aa034b3

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Models/UserModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function fetchIdentities(array $data): array
8585
// Map our users by ID to make assigning simpler
8686
$mappedUsers = [];
8787
$users = $data['singleton']
88-
? $data
88+
? [$data['data']]
8989
: $data['data'];
9090

9191
foreach ($users as $user) {
@@ -100,7 +100,7 @@ protected function fetchIdentities(array $data): array
100100
$mappedUsers[$id->user_id]->identities = $array;
101101
}
102102

103-
$data['data'] = $mappedUsers;
103+
$data['data'] = $data['singleton'] ? $mappedUsers[$id->user_id] : $mappedUsers;
104104

105105
return $data;
106106
}

tests/Unit/UserTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testGetIdentitiesByType(): void
5353
$this->assertEmpty($this->user->getIdentities('foo'));
5454
}
5555

56-
public function testModelWithIdentities(): void
56+
public function testModelfindAllWithIdentities(): void
5757
{
5858
fake(UserModel::class);
5959
fake(UserIdentityModel::class, ['user_id' => $this->user->id, 'type' => 'password']);
@@ -67,6 +67,18 @@ public function testModelWithIdentities(): void
6767
$this->assertCount(2, $identities);
6868
}
6969

70+
public function testModelfindByIdWithIdentities(): void
71+
{
72+
fake(UserModel::class);
73+
fake(UserIdentityModel::class, ['user_id' => $this->user->id, 'type' => 'password']);
74+
fake(UserIdentityModel::class, ['user_id' => $this->user->id, 'type' => 'access_token']);
75+
76+
// Grab the user again, using the model's identity helper
77+
$user = model(UserModel::class)->withIdentities()->findById(1);
78+
79+
$this->assertCount(2, $user->identities);
80+
}
81+
7082
public function testLastLogin(): void
7183
{
7284
fake(

0 commit comments

Comments
 (0)