Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.1] Fixed aggregate queries #14793

Merged
merged 2 commits into from
Aug 12, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ public function count($columns = '*')
*/
public function min($column)
{
return $this->aggregate(__FUNCTION__, [$column]);
return $this->formatedAggregate(__FUNCTION__, [$column]);
}

/**
Expand All @@ -1679,7 +1679,7 @@ public function min($column)
*/
public function max($column)
{
return $this->aggregate(__FUNCTION__, [$column]);
return $this->formatedAggregate(__FUNCTION__, [$column]);
}

/**
Expand All @@ -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]);
}

/**
Expand All @@ -1703,7 +1701,7 @@ public function sum($column)
*/
public function avg($column)
{
return $this->aggregate(__FUNCTION__, [$column]);
return $this->formatedAggregate(__FUNCTION__, [$column]);
}

/**
Expand All @@ -1722,7 +1720,7 @@ public function average($column)
*
* @param string $function
* @param array $columns
* @return float|int
* @return mixed
*/
public function aggregate($function, $columns = ['*'])
{
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may now become int|float

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeh, oops, Meant to put that :)

*/
protected function formatAggregate($results)
public function formatedAggregate($function, $columns = ['*'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably make that formattedAggregate (typo fix) or numberAggregate for more descriptive name.

{
$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;
Expand Down