From a744465a8cc2b9bd381ab4945505d87b4ceb9041 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 12 Aug 2016 18:58:43 +0100 Subject: [PATCH 1/2] Fixed aggregate queries --- src/Illuminate/Database/Query/Builder.php | 31 ++++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 94465d66ace7..8d47d33fe9d4 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -1668,7 +1668,7 @@ public function count($columns = '*') */ public function min($column) { - return $this->aggregate(__FUNCTION__, [$column]); + return $this->formatedAggregate(__FUNCTION__, [$column]); } /** @@ -1679,7 +1679,7 @@ public function min($column) */ public function max($column) { - return $this->aggregate(__FUNCTION__, [$column]); + return $this->formatedAggregate(__FUNCTION__, [$column]); } /** @@ -1690,9 +1690,7 @@ public function max($column) */ public function sum($column) { - $result = $this->aggregate(__FUNCTION__, [$column]); - - return $result ?: 0; + return $this->formatedAggregate(__FUNCTION__, [$column]); } /** @@ -1703,7 +1701,7 @@ public function sum($column) */ public function avg($column) { - return $this->aggregate(__FUNCTION__, [$column]); + return $this->formatedAggregate(__FUNCTION__, [$column]); } /** @@ -1722,7 +1720,7 @@ public function average($column) * * @param string $function * @param array $columns - * @return float|int + * @return mixed */ public function aggregate($function, $columns = ['*']) { @@ -1749,21 +1747,24 @@ public function aggregate($function, $columns = ['*']) $this->bindings['select'] = $previousSelectBindings; if (isset($results[0])) { - return $this->formatAggregate($results); + return array_change_key_case((array) $results[0])['aggregate']; } - - return 0; } /** - * Format the return value of an aggregate function. + * Execute and format an aggregate function on the database. * - * @param array $results - * @return float|int + * @param string $function + * @param array $columns + * @return mixed */ - protected function formatAggregate($results) + public function formatedAggregate($function, $columns = ['*']) { - $result = array_change_key_case((array) $results[0])['aggregate']; + $result = $this->aggregate($function, $columns); + + if (! $result) { + return 0; + } if (is_int($result) || is_float($result)) { return $result; From b8a8ebe06ff5cc45f255c27c79fd66c593eabd8b Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 12 Aug 2016 19:12:05 +0100 Subject: [PATCH 2/2] Tweaks --- src/Illuminate/Database/Query/Builder.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 8d47d33fe9d4..1e16a1544db8 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -1668,7 +1668,7 @@ public function count($columns = '*') */ public function min($column) { - return $this->formatedAggregate(__FUNCTION__, [$column]); + return $this->numericAggregate(__FUNCTION__, [$column]); } /** @@ -1679,7 +1679,7 @@ public function min($column) */ public function max($column) { - return $this->formatedAggregate(__FUNCTION__, [$column]); + return $this->numericAggregate(__FUNCTION__, [$column]); } /** @@ -1690,7 +1690,7 @@ public function max($column) */ public function sum($column) { - return $this->formatedAggregate(__FUNCTION__, [$column]); + return $this->numericAggregate(__FUNCTION__, [$column]); } /** @@ -1701,7 +1701,7 @@ public function sum($column) */ public function avg($column) { - return $this->formatedAggregate(__FUNCTION__, [$column]); + return $this->numericAggregate(__FUNCTION__, [$column]); } /** @@ -1752,13 +1752,13 @@ public function aggregate($function, $columns = ['*']) } /** - * Execute and format an aggregate function on the database. + * Execute a numeric aggregate function on the database. * * @param string $function * @param array $columns - * @return mixed + * @return float|int */ - public function formatedAggregate($function, $columns = ['*']) + public function numericAggregate($function, $columns = ['*']) { $result = $this->aggregate($function, $columns);