Skip to content

Commit

Permalink
Merge pull request #28 from Weebly/wherein-subquery
Browse files Browse the repository at this point in the history
Deeper nested wheres
  • Loading branch information
khepin authored Jan 24, 2020
2 parents 7b20796 + 4686862 commit b96b860
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Database/EloquentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
return $this;
}

if ($where['type'] === 'InSub' || $where['type'] === 'NotInSub') {
$this->query->wheres[] = $where;

return parent::whereIn($column, $values, $boolean, $not);
}

// Get the column name
$mutatedColumn = $this->getUnqualifiedColumnName($where['column']);

Expand Down
22 changes: 21 additions & 1 deletion tests/Integration/MutatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,25 @@ public function test_where_in_subquery()
(new TestModel())->create(['id' => $id, 'name' => 'A chair']);
(new TestModel())->create(['id' => $id2, 'name' => 'A table']);
$p = TestModel::whereIn('id', function ($query) {
$query->select('id')->from('test_model');
$query->select('id')->from('test_model')->whereNull('location');
})->get();
$this->assertEquals(2, $p->count());
$this->assertEquals($id, $p->first()->id);
$this->assertEquals($id2, $p->last()->id);
}

public function test_where_in_wherein_subquery()
{
$id = Uuid::uuid1()->toString();
$id2 = Uuid::uuid1()->toString();
(new TestModel())->create(['id' => $id, 'name' => 'A chair']);
(new TestModel())->create(['id' => $id2, 'name' => 'A table']);
$p = TestModel::withWherein()->get();
$this->assertEquals(2, $p->count());
$this->assertEquals($id, $p->first()->id);
$this->assertEquals($id2, $p->last()->id);
}

public function test_where_not_in()
{
$id = Uuid::uuid1()->toString();
Expand Down Expand Up @@ -256,6 +268,14 @@ class TestModel extends Model
'id' => 'uuid_v1_binary',
'location' => 'encrypt_string',
];

public function scopeWithWherein($builder)
{
$builder->whereIn('id', function ($subquery) {
$subquery->select('id')->from('test_model')
->whereNull('location');
});
}
}

class TimestampedModel extends Model
Expand Down

0 comments on commit b96b860

Please sign in to comment.