Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix whereIn subquery for Laravel 5.8+ #29

Merged
merged 4 commits into from
Oct 29, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
/.idea
composer.lock
.phpunit.result.cache
15 changes: 12 additions & 3 deletions src/Database/EloquentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Weebly\Mutate\Database;

use Illuminate\Database\Eloquent\Builder;
use Closure;
use Illuminate\Database\Eloquent\Builder as BaseEloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Str;

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

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

// For sub-queries in Laravel 6. We don't need to do anything.
if ($values instanceof QueryBuilder ||
$values instanceof BaseEloquentBuilder ||
$values instanceof Closure) {
return $this;
}

// Remove the last item of the array
$where = array_pop($this->query->wheres);

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

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

// Get the column name
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/MutatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ public function test_where_in_subquery()
$this->assertEquals($id2, $p->last()->id);
}

public function test_where_in_subquery_with_bindings()
{
$id = Uuid::uuid1()->toString();
$id2 = Uuid::uuid1()->toString();
(new TestModel())->create(['id' => $id, 'name' => 'A chair', 'location' => 'One']);
(new TestModel())->create(['id' => $id2, 'name' => 'A table', 'location' => 'Two']);

$query = TestModel::query()->where('name', '!=', 'A lamp')->whereIn('id', function ($query) {
$query->select('id')->from('test_model')->where('name', 'A lamp')->orWhere('name', 'A chair');
});

$p = $query->get();
$this->assertEquals(1, $p->count());
$this->assertEquals($id, $p->first()->id);
}

public function test_where_in_wherein_subquery()
{
$id = Uuid::uuid1()->toString();
Expand Down