diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index 49fefdbbce63..fc384662de03 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -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).')'; } diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index e6e4885458ad..47b3531e0650 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -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; @@ -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();