Skip to content

Commit

Permalink
[10.x] Fix nested join when not JoinClause instance (#46712)
Browse files Browse the repository at this point in the history
* Fix nested join when not JoinClause instance

* reduce change

* Fix changes
  • Loading branch information
joelharkes authored Apr 10, 2023
1 parent 4d178bd commit 1e129e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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

0 comments on commit 1e129e6

Please sign in to comment.