Skip to content

Commit 317f702

Browse files
The-HasanovHikmat Hasanov
and
Hikmat Hasanov
authored
fix: whereBelongsTo (#2454)
* override getQualifiedForeignKeyName() and add tests for whereBelongsTo * delete unneeded query() call * type hint for getQualifiedForeignKeyName() Co-authored-by: Hikmat Hasanov <[email protected]>
1 parent e5e9193 commit 317f702

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Diff for: src/Relations/BelongsTo.php

+5
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ protected function whereInMethod(EloquentModel $model, $key)
7272
{
7373
return 'whereIn';
7474
}
75+
76+
public function getQualifiedForeignKeyName(): string
77+
{
78+
return $this->foreignKey;
79+
}
7580
}

Diff for: tests/RelationsTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -534,4 +534,17 @@ public function testDoubleSaveManyToMany(): void
534534
$this->assertEquals([$user->_id], $client->user_ids);
535535
$this->assertEquals([$client->_id], $user->client_ids);
536536
}
537+
538+
public function testWhereBelongsTo()
539+
{
540+
$user = User::create(['name' => 'John Doe']);
541+
Item::create(['user_id' => $user->_id]);
542+
Item::create(['user_id' => $user->_id]);
543+
Item::create(['user_id' => $user->_id]);
544+
Item::create(['user_id' => null]);
545+
546+
$items = Item::whereBelongsTo($user)->get();
547+
548+
$this->assertCount(3, $items);
549+
}
537550
}

0 commit comments

Comments
 (0)