-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[5.5] Add Eloquent Features: withSum,withMax,withMin,withAvg etc. #19363
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,6 +201,117 @@ public function withCount($relations) | |
return $this; | ||
} | ||
|
||
/** | ||
* Add subselect queries to sum the relations. | ||
* | ||
* @param mixed $relations | ||
* @return $this | ||
*/ | ||
public function withSum($relations) | ||
{ | ||
$relations = is_array($relations) ? $relations : func_get_args(); | ||
|
||
return $this->withAggregate($relations, 'SUM'); | ||
} | ||
|
||
/** | ||
* Add subselect queries to max the relations. | ||
* | ||
* @param mixed $relations | ||
* @return $this | ||
*/ | ||
public function withMax($relations) | ||
{ | ||
$relations = is_array($relations) ? $relations : func_get_args(); | ||
|
||
return $this->withAggregate($relations, 'MAX'); | ||
} | ||
|
||
/** | ||
* Add subselect queries to min the relations. | ||
* | ||
* @param mixed $relations | ||
* @return $this | ||
*/ | ||
public function withMin($relations) | ||
{ | ||
$relations = is_array($relations) ? $relations : func_get_args(); | ||
|
||
return $this->withAggregate($relations, 'MIN'); | ||
} | ||
|
||
/** | ||
* Add subselect queries to min the relations. | ||
* | ||
* @param mixed $relations | ||
* @return $this | ||
*/ | ||
public function withAvg($relations) | ||
{ | ||
$relations = is_array($relations) ? $relations : func_get_args(); | ||
|
||
return $this->withAggregate($relations, 'AVG'); | ||
} | ||
|
||
/** | ||
* use the MySQL aggregate functions including AVG COUNT, SUM, MAX and MIN. | ||
* | ||
* @param array $relations | ||
* @param string $function | ||
* @return $this | ||
*/ | ||
public function withAggregate($relations, $function = 'COUNT') | ||
{ | ||
if (is_null($this->query->columns)) { | ||
$this->query->select([$this->query->from.'.*']); | ||
} | ||
|
||
// set to lower | ||
$function = Str::lower($function); | ||
|
||
foreach ($this->parseWithRelations($relations) as $name => $constraints) { | ||
// First we will determine if the name has been aliased using an "as" clause on the name | ||
// and if it has we will extract the actual relationship name and the desired name of | ||
// the resulting column. This allows multiple counts on the same relationship name. | ||
$segments = explode(' ', $name); | ||
|
||
unset($alias); | ||
|
||
if (count($segments) == 3 && Str::lower($segments[1]) == 'as') { | ||
list($name, $alias) = [$segments[0], $segments[2]]; | ||
} | ||
|
||
// set the default column as * or primary key | ||
$column = ($function == 'count') ? '*' : $this->model->getKeyName(); | ||
|
||
if (strpos($name, '|') !== false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double negatives if (Str::contains($name, '|')) { /* ... */ } Pipe vs. comma |
||
list($name, $column) = explode('|', $name); | ||
} | ||
|
||
$relation = $this->getRelationWithoutConstraints($name); | ||
|
||
// Here we will get the relationship count query and prepare to add it to the main query | ||
// as a sub-select. First, we'll get the "has" query and use that to get the relation | ||
// count query. We will normalize the relation name then append _count as the name. | ||
$query = $relation->getRelationExistenceAggregateQuery( | ||
$relation->getRelated()->newQuery(), $this, $function, $column | ||
); | ||
|
||
$query->callScope($constraints); | ||
|
||
$query->mergeConstraintsFrom($relation->getQuery()); | ||
|
||
// 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 = snake_case(isset($alias) ? $alias : $name).'_'.$function; | ||
|
||
$this->selectSub($query->toBase(), $column); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add the "has" condition where clause to the query. | ||
* | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first comment in this review
I would remove the following comment, since the code speaks for it self. 👍