Skip to content

Commit b96b860

Browse files
authored
Merge pull request #28 from Weebly/wherein-subquery
Deeper nested wheres
2 parents 7b20796 + 4686862 commit b96b860

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Database/EloquentBuilder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
9393
return $this;
9494
}
9595

96+
if ($where['type'] === 'InSub' || $where['type'] === 'NotInSub') {
97+
$this->query->wheres[] = $where;
98+
99+
return parent::whereIn($column, $values, $boolean, $not);
100+
}
101+
96102
// Get the column name
97103
$mutatedColumn = $this->getUnqualifiedColumnName($where['column']);
98104

tests/Integration/MutatorTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,25 @@ public function test_where_in_subquery()
125125
(new TestModel())->create(['id' => $id, 'name' => 'A chair']);
126126
(new TestModel())->create(['id' => $id2, 'name' => 'A table']);
127127
$p = TestModel::whereIn('id', function ($query) {
128-
$query->select('id')->from('test_model');
128+
$query->select('id')->from('test_model')->whereNull('location');
129129
})->get();
130130
$this->assertEquals(2, $p->count());
131131
$this->assertEquals($id, $p->first()->id);
132132
$this->assertEquals($id2, $p->last()->id);
133133
}
134134

135+
public function test_where_in_wherein_subquery()
136+
{
137+
$id = Uuid::uuid1()->toString();
138+
$id2 = Uuid::uuid1()->toString();
139+
(new TestModel())->create(['id' => $id, 'name' => 'A chair']);
140+
(new TestModel())->create(['id' => $id2, 'name' => 'A table']);
141+
$p = TestModel::withWherein()->get();
142+
$this->assertEquals(2, $p->count());
143+
$this->assertEquals($id, $p->first()->id);
144+
$this->assertEquals($id2, $p->last()->id);
145+
}
146+
135147
public function test_where_not_in()
136148
{
137149
$id = Uuid::uuid1()->toString();
@@ -256,6 +268,14 @@ class TestModel extends Model
256268
'id' => 'uuid_v1_binary',
257269
'location' => 'encrypt_string',
258270
];
271+
272+
public function scopeWithWherein($builder)
273+
{
274+
$builder->whereIn('id', function ($subquery) {
275+
$subquery->select('id')->from('test_model')
276+
->whereNull('location');
277+
});
278+
}
259279
}
260280

261281
class TimestampedModel extends Model

0 commit comments

Comments
 (0)