diff --git a/Query/Builder.php b/Query/Builder.php index ddd637754..4d45046b3 100755 --- a/Query/Builder.php +++ b/Query/Builder.php @@ -1550,33 +1550,6 @@ public function addNestedWhereQuery($query, $boolean = 'and') return $this; } - public function havingNested(Closure $callback, $boolean = 'and') - { - call_user_func($callback, $query = $this->forNestedWhere()); - - return $this->addNestedHavingQuery($query, $boolean); - } - - /** - * Add another query builder as a nested where to the query builder. - * - * @param \Illuminate\Database\Query\Builder $query - * @param string $boolean - * @return $this - */ - public function addNestedHavingQuery($query, $boolean = 'and') - { - if (count($query->havings)) { - $type = 'Nested'; - - $this->havings[] = compact('type', 'query', 'boolean'); - - $this->addBinding($query->getRawBindings()['having'], 'having'); - } - - return $this; - } - /** * Add a full sub-select to the query. * @@ -1968,9 +1941,6 @@ public function having($column, $operator = null, $value = null, $boolean = 'and $value, $operator, func_num_args() === 2 ); - // If the columns is actually a Closure instance, we will assume the developer - // wants to begin a nested having statement which is wrapped in parenthesis. - // We'll add that Closure to the query then return back out immediately. if ($column instanceof Closure && is_null($operator)) { return $this->havingNested($column, $boolean); } @@ -2012,6 +1982,40 @@ public function orHaving($column, $operator = null, $value = null) return $this->having($column, $operator, $value, 'or'); } + /** + * Add a nested having statement to the query. + * + * @param \Closure $callback + * @param string $boolean + * @return $this + */ + public function havingNested(Closure $callback, $boolean = 'and') + { + call_user_func($callback, $query = $this->forNestedWhere()); + + return $this->addNestedHavingQuery($query, $boolean); + } + + /** + * Add another query builder as a nested having to the query builder. + * + * @param \Illuminate\Database\Query\Builder $query + * @param string $boolean + * @return $this + */ + public function addNestedHavingQuery($query, $boolean = 'and') + { + if (count($query->havings)) { + $type = 'Nested'; + + $this->havings[] = compact('type', 'query', 'boolean'); + + $this->addBinding($query->getRawBindings()['having'], 'having'); + } + + return $this; + } + /** * Add a "having null" clause to the query. * diff --git a/Query/Grammars/Grammar.php b/Query/Grammars/Grammar.php index 38e6e638f..89b528382 100755 --- a/Query/Grammars/Grammar.php +++ b/Query/Grammars/Grammar.php @@ -688,11 +688,9 @@ protected function compileGroups(Builder $query, $groups) */ protected function compileHavings(Builder $query) { - $sql = collect($query->havings)->map(function ($having) { + return 'having '.$this->removeLeadingBoolean(collect($query->havings)->map(function ($having) { return $having['boolean'].' '.$this->compileHaving($having); - })->implode(' '); - - return 'having '.$this->removeLeadingBoolean($sql); + })->implode(' ')); } /** @@ -724,17 +722,6 @@ protected function compileHaving(array $having) return $this->compileBasicHaving($having); } - /** - * Compile a nested having clause. - * - * @param array $having - * @return string - */ - protected function compileNestedHavings($having) - { - return '('.substr($this->compileHavings($having['query']), 7).')'; - } - /** * Compile a basic having clause. * @@ -810,6 +797,17 @@ protected function compileHavingBit($having) return '('.$column.' '.$having['operator'].' '.$parameter.') != 0'; } + /** + * Compile a nested having clause. + * + * @param array $having + * @return string + */ + protected function compileNestedHavings($having) + { + return '('.substr($this->compileHavings($having['query']), 7).')'; + } + /** * Compile the "order by" portions of the query. *