Skip to content

Commit

Permalink
Add loadAggregate() and load[min(), max(), sum() & avg()] methods to …
Browse files Browse the repository at this point in the history
…Eloquent/{Collection, Model} (#35029)

Also add loadMorphAggregate() and loadMorph[min(), max(), sum() & avg()] methods to Eloquent/Model
  • Loading branch information
khalilst authored Oct 30, 2020
1 parent 3a0f1fd commit 363dc94
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 8 deletions.
67 changes: 64 additions & 3 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ public function load($relations)
}

/**
* Load a set of relationship counts onto the collection.
* Load a set of aggregations over relationship's column onto the collection.
*
* @param array|string $relations
* @param string $column
* @param string $function
* @return $this
*/
public function loadCount($relations)
public function loadAggregate($relations, $column, $function = null)
{
if ($this->isEmpty()) {
return $this;
Expand All @@ -78,7 +80,7 @@ public function loadCount($relations)
$models = $this->first()->newModelQuery()
->whereKey($this->modelKeys())
->select($this->first()->getKeyName())
->withCount(...func_get_args())
->withAggregate($relations, $column, $function)
->get()
->keyBy($this->first()->getKeyName());

Expand All @@ -96,6 +98,65 @@ public function loadCount($relations)
return $this;
}

/**
* Load a set of relationship counts onto the collection.
*
* @param array|string $relations
* @return $this
*/
public function loadCount($relations)
{
return $this->loadAggregate($relations, '*', 'count');
}

/**
* Load a set of relationship's max column values onto the collection.
*
* @param array|string $relations
* @param string $column
* @return $this
*/
public function loadMax($relations, $column)
{
return $this->loadAggregate($relations, $column, 'max');
}

/**
* Load a set of relationship's min column values onto the collection.
*
* @param array|string $relations
* @param string $column
* @return $this
*/
public function loadMin($relations, $column)
{
return $this->loadAggregate($relations, $column, 'min');
}

/**
* Load a set of relationship's column summations onto the collection.
*
* @param array|string $relations
* @param string $column
* @return $this
*/
public function loadSum($relations, $column)
{
return $this->loadAggregate($relations, $column, 'sum');
}

/**
* Load a set of relationship's average column values onto the collection.
*
* @param array|string $relations
* @param string $column
* @return $this
*/
public function loadAvg($relations, $column)
{
return $this->loadAggregate($relations, $column, 'avg');
}

/**
* Load a set of relationships onto the collection if they are not already eager loaded.
*
Expand Down
137 changes: 132 additions & 5 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,21 @@ public function loadMissing($relations)
return $this;
}

/**
* Eager load relation's column aggregations on the model.
*
* @param array|string $relations
* @param string $column
* @param string $function
* @return $this
*/
public function loadAggregate($relations, $column, $function = null)
{
$this->newCollection([$this])->loadAggregate($relations, $column, $function);

return $this;
}

/**
* Eager load relation counts on the model.
*
Expand All @@ -552,31 +567,143 @@ public function loadCount($relations)
{
$relations = is_string($relations) ? func_get_args() : $relations;

$this->newCollection([$this])->loadCount($relations);
return $this->loadAggregate($relations, '*', 'count');
}

return $this;
/**
* Eager load relation max column values on the model.
*
* @param array|string $relations
* @param string $column
* @return $this
*/
public function loadMax($relations, $column)
{
return $this->loadAggregate($relations, $column, 'max');
}

/**
* Eager load relationship counts on the polymorphic relation of a model.
* Eager load relation min column values on the model.
*
* @param array|string $relations
* @param string $column
* @return $this
*/
public function loadMin($relations, $column)
{
return $this->loadAggregate($relations, $column, 'min');
}

/**
* Eager load relation's column summations on the model.
*
* @param array|string $relations
* @param string $column
* @return $this
*/
public function loadSum($relations, $column)
{
return $this->loadAggregate($relations, $column, 'sum');
}

/**
* Eager load relation average column values on the model.
*
* @param array|string $relations
* @param string $column
* @return $this
*/
public function loadAvg($relations, $column)
{
return $this->loadAggregate($relations, $column, 'avg');
}

/**
* Eager load relationship column aggregation on the polymorphic relation of a model.
*
* @param string $relation
* @param array $relations
* @param string $column
* @param string $function
* @return $this
*/
public function loadMorphCount($relation, $relations)
public function loadMorphAggregate($relation, $relations, $column, $function = null)
{
if (! $this->{$relation}) {
return $this;
}

$className = get_class($this->{$relation});

$this->{$relation}->loadCount($relations[$className] ?? []);
$this->{$relation}->loadAggregate($relations[$className] ?? [], $column, $function);

return $this;
}

/**
* Eager load relationship counts on the polymorphic relation of a model.
*
* @param string $relation
* @param array $relations
* @return $this
*/
public function loadMorphCount($relation, $relations)
{
return $this->loadMorphAggregate($relation, $relations, '*', 'count');
}

/**
* Eager load relationship max column values on the polymorphic relation of a model.
*
* @param string $relation
* @param array $relations
* @param string $column
* @return $this
*/
public function loadMorphMax($relation, $relations, $column)
{
return $this->loadMorphAggregate($relation, $relations, $column, 'max');
}

/**
* Eager load relationship min column values on the polymorphic relation of a model.
*
* @param string $relation
* @param array $relations
* @param string $column
* @return $this
*/
public function loadMorphMin($relation, $relations, $column)
{
return $this->loadMorphAggregate($relation, $relations, $column, 'min');
}

/**
* Eager load relationship column summations on the polymorphic relation of a model.
*
* @param string $relation
* @param array $relations
* @param string $column
* @return $this
*/
public function loadMorphSum($relation, $relations, $column)
{
return $this->loadMorphAggregate($relation, $relations, $column, 'sum');
}

/**
* Eager load relationship average column values on the polymorphic relation of a model.
*
* @param string $relation
* @param array $relations
* @param string $column
* @return $this
*/
public function loadMorphAvg($relation, $relations, $column)
{
return $this->loadMorphAggregate($relation, $relations, $column, 'avg');
}

/**
* Increment a column's value by a given amount.
*
Expand Down

0 comments on commit 363dc94

Please sign in to comment.