Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ public function whereKey(mixed $id, string $boolean = 'and', bool $not = false):
$id = $id->getKey();
}

if ($id instanceof Closure || $id instanceof QueryBuilder || $id instanceof self || $id instanceof Relation) {
$this->query->whereIn($this->model->getQualifiedKeyName(), $id, $boolean, $not);

return $this;
}

if (is_array($id) || $id instanceof Arrayable) {
if (in_array($this->model->getKeyType(), ['int', 'integer'])) {
$this->query->whereIntegerInRaw($this->model->getQualifiedKeyName(), $id, $boolean, $not);
Expand Down
37 changes: 37 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,31 @@ public function testWhereKeyMethodWithCollection()
$builder->whereKey($collection);
}

public function testWhereKeyMethodWithClosure()
{
$model = new EloquentBuilderTestStub;
$this->mockConnectionForModel($model, 'SQLite');

$query = $model->newQuery()->whereKey(function ($query) {
$query->select('id')->from('users')->where('active', true);
});

$this->assertSame('select * from "table" where "table"."id" in (select "id" from "users" where "active" = ?)', $query->toSql());
$this->assertEquals([true], $query->getBindings());
}

public function testWhereKeyMethodWithSubquery()
{
$model = new EloquentBuilderTestStub;
$this->mockConnectionForModel($model, 'SQLite');

$subquery = $model->newQuery()->select('id')->where('active', true);
$query = $model->newQuery()->whereKey($subquery);

$this->assertSame('select * from "table" where "table"."id" in (select "id" from "table" where "active" = ?)', $query->toSql());
$this->assertEquals([true], $query->getBindings());
}

public function testWhereKeyMethodWithModel()
{
$model = new EloquentBuilderTestStubStringPrimaryKey;
Expand Down Expand Up @@ -2494,6 +2519,18 @@ public function testWhereKeyNotMethodWithCollection()
$builder->whereKeyNot($collection);
}

public function testWhereKeyNotMethodWithSubquery()
{
$model = new EloquentBuilderTestStub;
$this->mockConnectionForModel($model, 'SQLite');

$subquery = $model->newQuery()->select('id')->where('active', true);
$query = $model->newQuery()->whereKeyNot($subquery);

$this->assertSame('select * from "table" where "table"."id" not in (select "id" from "table" where "active" = ?)', $query->toSql());
$this->assertEquals([true], $query->getBindings());
}

public function testWhereKeyNotMethodWithModel()
{
$model = new EloquentBuilderTestStubStringPrimaryKey;
Expand Down