diff --git a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php index 289770375f94..5f2ec2dab7b9 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php @@ -210,14 +210,18 @@ public function withCount($relations) $query->callScope($constraints); - $query->mergeConstraintsFrom($relation->getQuery()); + $query = $query->mergeConstraintsFrom($relation->getQuery())->toBase(); + + if (count($query->columns) > 1) { + $query->columns = [$query->columns[0]]; + } // Finally we will add the proper result column alias to the query and run the subselect // statement against the query builder. Then we will return the builder instance back // to the developer for further constraint chaining that needs to take place on it. $column = $alias ?? Str::snake($name.'_count'); - $this->selectSub($query->toBase(), $column); + $this->selectSub($query, $column); } return $this; diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 2bbddfd066f9..1025915ed919 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -279,8 +279,6 @@ public function selectSub($query, $as) protected function parseSubSelect($query) { if ($query instanceof self) { - $query->columns = [$query->columns[0]]; - return [$query->toSql(), $query->getBindings()]; } elseif (is_string($query)) { return [$query, []]; diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 49d514b6bd67..02ca507f9f38 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -1449,10 +1449,11 @@ public function testAggregateWithSubSelect() return $results; }); $builder->from('users')->selectSub(function ($query) { - $query->from('posts')->select('foo')->where('title', 'foo'); + $query->from('posts')->select('foo', 'bar')->where('title', 'foo'); }, 'post'); $count = $builder->count(); $this->assertEquals(1, $count); + $this->assertEquals('(select "foo", "bar" from "posts" where "title" = ?) as "post"', $builder->columns[0]->getValue()); $this->assertEquals(['foo'], $builder->getBindings()); }