Skip to content

Commit

Permalink
fix: whereBelongsTo (#2454)
Browse files Browse the repository at this point in the history
* override getQualifiedForeignKeyName() and add tests for whereBelongsTo

* delete unneeded query() call

* type hint for getQualifiedForeignKeyName()

Co-authored-by: Hikmat Hasanov <hikmet.hasanov@proton.me>
  • Loading branch information
The-Hasanov and Hikmat Hasanov committed Jan 15, 2023
1 parent e5e9193 commit 317f702
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ protected function whereInMethod(EloquentModel $model, $key)
{
return 'whereIn';
}

public function getQualifiedForeignKeyName(): string
{
return $this->foreignKey;
}
}
13 changes: 13 additions & 0 deletions tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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::whereBelongsTo($user)->get();

$this->assertCount(3, $items);
}
}

0 comments on commit 317f702

Please sign in to comment.