Skip to content

Commit 1e129e6

Browse files
authored
[10.x] Fix nested join when not JoinClause instance (#46712)
* Fix nested join when not JoinClause instance * reduce change * Fix changes
1 parent 4d178bd commit 1e129e6

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Illuminate/Database/Query/Grammars/Grammar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ protected function whereNested(Builder $query, $where)
504504
// Here we will calculate what portion of the string we need to remove. If this
505505
// is a join clause query, we need to remove the "on" portion of the SQL and
506506
// if it is a normal query we need to take the leading "where" of queries.
507-
$offset = $query instanceof JoinClause ? 3 : 6;
507+
$offset = $where['query'] instanceof JoinClause ? 3 : 6;
508508

509509
return '('.substr($this->compileWheres($where['query']), $offset).')';
510510
}

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Database\Query\Grammars\PostgresGrammar;
1515
use Illuminate\Database\Query\Grammars\SQLiteGrammar;
1616
use Illuminate\Database\Query\Grammars\SqlServerGrammar;
17+
use Illuminate\Database\Query\JoinClause;
1718
use Illuminate\Database\Query\Processors\MySqlProcessor;
1819
use Illuminate\Database\Query\Processors\PostgresProcessor;
1920
use Illuminate\Database\Query\Processors\Processor;
@@ -2425,6 +2426,18 @@ public function testJoinsWithNestedJoinWithAdvancedSubqueryCondition()
24252426
$this->assertEquals(['1', 10000], $builder->getBindings());
24262427
}
24272428

2429+
public function testJoinWithNestedOnCondition()
2430+
{
2431+
$builder = $this->getBuilder();
2432+
$builder->select('users.id')->from('users')->join('contacts', function (JoinClause $j) {
2433+
return $j
2434+
->on('users.id', 'contacts.id')
2435+
->addNestedWhereQuery($this->getBuilder()->where('contacts.id', 1));
2436+
});
2437+
$this->assertSame('select "users"."id" from "users" inner join "contacts" on "users"."id" = "contacts"."id" and ("contacts"."id" = ?)', $builder->toSql());
2438+
$this->assertEquals([1], $builder->getBindings());
2439+
}
2440+
24282441
public function testJoinSub()
24292442
{
24302443
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)