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

[10.x] Fix nested join when not JoinClause instance #46712

Merged
merged 3 commits into from
Apr 10, 2023
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ protected function whereNested(Builder $query, $where)
// Here we will calculate what portion of the string we need to remove. If this
// is a join clause query, we need to remove the "on" portion of the SQL and
// if it is a normal query we need to take the leading "where" of queries.
$offset = $query instanceof JoinClause ? 3 : 6;
$offset = $where['query'] instanceof JoinClause ? 3 : 6;

return '('.substr($this->compileWheres($where['query']), $offset).')';
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Database\Query\Grammars\PostgresGrammar;
use Illuminate\Database\Query\Grammars\SQLiteGrammar;
use Illuminate\Database\Query\Grammars\SqlServerGrammar;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Database\Query\Processors\MySqlProcessor;
use Illuminate\Database\Query\Processors\PostgresProcessor;
use Illuminate\Database\Query\Processors\Processor;
Expand Down Expand Up @@ -2425,6 +2426,18 @@ public function testJoinsWithNestedJoinWithAdvancedSubqueryCondition()
$this->assertEquals(['1', 10000], $builder->getBindings());
}

public function testJoinWithNestedOnCondition()
{
$builder = $this->getBuilder();
$builder->select('users.id')->from('users')->join('contacts', function (JoinClause $j) {
return $j
->on('users.id', 'contacts.id')
->addNestedWhereQuery($this->getBuilder()->where('contacts.id', 1));
});
$this->assertSame('select "users"."id" from "users" inner join "contacts" on "users"."id" = "contacts"."id" and ("contacts"."id" = ?)', $builder->toSql());
$this->assertEquals([1], $builder->getBindings());
}

public function testJoinSub()
{
$builder = $this->getBuilder();
Expand Down