From f3fab09751c59f0ac0c8856b6ff7596fae399892 Mon Sep 17 00:00:00 2001 From: Hikmat Hasanov Date: Wed, 12 Oct 2022 14:14:08 +0400 Subject: [PATCH 1/3] override getQualifiedForeignKeyName() and add tests for whereBelongsTo --- src/Relations/BelongsTo.php | 8 ++++++++ tests/RelationsTest.php | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Relations/BelongsTo.php b/src/Relations/BelongsTo.php index adaa13110..94d1f69ce 100644 --- a/src/Relations/BelongsTo.php +++ b/src/Relations/BelongsTo.php @@ -72,4 +72,12 @@ protected function whereInMethod(EloquentModel $model, $key) { return 'whereIn'; } + + /** + * @return string + */ + public function getQualifiedForeignKeyName() + { + return $this->foreignKey; + } } diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 59d0f2757..18e50faef 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -534,4 +534,17 @@ public function testDoubleSaveManyToMany(): void $this->assertEquals([$user->_id], $client->user_ids); $this->assertEquals([$client->_id], $user->client_ids); } + + public function testWhereBelongsTo() + { + $user = User::create(['name' => 'John Doe']); + Item::create(['user_id' => $user->_id]); + Item::create(['user_id' => $user->_id]); + Item::create(['user_id' => $user->_id]); + Item::create(['user_id' => null]); + + $items = Item::query()->whereBelongsTo($user)->get(); + + $this->assertCount(3, $items); + } } From 4bec4d357c86e5b6397e7dc07dce68c4304e93fe Mon Sep 17 00:00:00 2001 From: Hikmat Hasanov Date: Wed, 12 Oct 2022 14:24:43 +0400 Subject: [PATCH 2/3] delete unneeded query() call --- tests/RelationsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 18e50faef..c702f0e2b 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -543,7 +543,7 @@ public function testWhereBelongsTo() Item::create(['user_id' => $user->_id]); Item::create(['user_id' => null]); - $items = Item::query()->whereBelongsTo($user)->get(); + $items = Item::whereBelongsTo($user)->get(); $this->assertCount(3, $items); } From b66d767d579c34f2c8600e725cc9777476008979 Mon Sep 17 00:00:00 2001 From: Hikmat Hasanov Date: Sun, 20 Nov 2022 17:01:33 +0400 Subject: [PATCH 3/3] type hint for getQualifiedForeignKeyName() --- src/Relations/BelongsTo.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Relations/BelongsTo.php b/src/Relations/BelongsTo.php index 94d1f69ce..8a3690c47 100644 --- a/src/Relations/BelongsTo.php +++ b/src/Relations/BelongsTo.php @@ -73,10 +73,7 @@ protected function whereInMethod(EloquentModel $model, $key) return 'whereIn'; } - /** - * @return string - */ - public function getQualifiedForeignKeyName() + public function getQualifiedForeignKeyName(): string { return $this->foreignKey; }