From 80f1791ee4c305705aa1fe093ccba3eb89880401 Mon Sep 17 00:00:00 2001 From: Caleb White Date: Tue, 8 Sep 2026 08:50:29 -0500 Subject: [PATCH] feat: support subqueries in whereKey --- src/Illuminate/Database/Eloquent/Builder.php | 6 +++ .../Database/DatabaseEloquentBuilderTest.php | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index a1f13ea8ae0d..5d7e6c70235e 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -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); diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 6f0094cec0b3..a601e0b5b160 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -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; @@ -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;