Skip to content

Commit f91eb9d

Browse files
authored
Merge pull request #29 from Weebly/where-in-subquery-fix
Fix whereIn subquery for Laravel 5.8+
2 parents 0afe08e + 927509c commit f91eb9d

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor
22
/.idea
33
composer.lock
4+
.phpunit.result.cache

src/Database/EloquentBuilder.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Weebly\Mutate\Database;
44

5-
use Illuminate\Database\Eloquent\Builder;
5+
use Closure;
6+
use Illuminate\Database\Eloquent\Builder as BaseEloquentBuilder;
7+
use Illuminate\Database\Query\Builder as QueryBuilder;
68
use Illuminate\Database\Query\Expression;
79
use Illuminate\Support\Str;
810

9-
class EloquentBuilder extends Builder
11+
class EloquentBuilder extends BaseEloquentBuilder
1012
{
1113
/**
1214
* @var \Weebly\Mutate\Database\Model
@@ -83,6 +85,13 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
8385

8486
parent::whereIn($column, $values, $boolean, $not);
8587

88+
// For sub-queries in Laravel 6. We don't need to do anything.
89+
if ($values instanceof QueryBuilder ||
90+
$values instanceof BaseEloquentBuilder ||
91+
$values instanceof Closure) {
92+
return $this;
93+
}
94+
8695
// Remove the last item of the array
8796
$where = array_pop($this->query->wheres);
8897

@@ -96,7 +105,7 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
96105
if ($where['type'] === 'InSub' || $where['type'] === 'NotInSub') {
97106
$this->query->wheres[] = $where;
98107

99-
return parent::whereIn($column, $values, $boolean, $not);
108+
return $this;
100109
}
101110

102111
// Get the column name

tests/Integration/MutatorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ public function test_where_in_subquery()
132132
$this->assertEquals($id2, $p->last()->id);
133133
}
134134

135+
public function test_where_in_subquery_with_bindings()
136+
{
137+
$id = Uuid::uuid1()->toString();
138+
$id2 = Uuid::uuid1()->toString();
139+
(new TestModel())->create(['id' => $id, 'name' => 'A chair', 'location' => 'One']);
140+
(new TestModel())->create(['id' => $id2, 'name' => 'A table', 'location' => 'Two']);
141+
142+
$query = TestModel::query()->where('name', '!=', 'A lamp')->whereIn('id', function ($query) {
143+
$query->select('id')->from('test_model')->where('name', 'A lamp')->orWhere('name', 'A chair');
144+
});
145+
146+
$p = $query->get();
147+
$this->assertEquals(1, $p->count());
148+
$this->assertEquals($id, $p->first()->id);
149+
}
150+
135151
public function test_where_in_wherein_subquery()
136152
{
137153
$id = Uuid::uuid1()->toString();

0 commit comments

Comments
 (0)