laravel/framework · error · InvalidArgumentException
Invalid aggregate [ ] used within ofMany relation…
Error message
Invalid aggregate [{$aggregate}] used within ofMany relation. Available aggregates: MIN, MAX What it means
CanBeOneOfMany::ofMany() only supports MIN and MAX aggregates (it builds a sub-select joining on the min/max per group). Passing any other aggregate string ('SUM', 'AVG', 'COUNT', etc.) throws, because the SQL shape for those would not select a single representative row correctly.
Solutions
- Use only 'MIN' or 'MAX' (case-insensitive) as the aggregate.
- If you need AVG/SUM-style selection, write it as a custom relationship query or a join subquery outside ofMany.
- Re-read the ofMany signature: ofMany($column = 'id', $aggregate = 'MAX', $relation = null).
Example fix
// before
return $this->hasOne(OrderItem::class)->ofMany('price', 'AVG'); // throws
// after
return $this->hasOne(OrderItem::class)->ofMany('price', 'MAX'); // most expensive item
// or 'MIN' for cheapest Defensive patterns
Strategy: validation
Validate before calling
$allowed = ['min', 'max'];
if (! in_array(strtolower($aggregate), $allowed, true)) {
throw new \InvalidArgumentException("ofMany aggregate must be one of: " . implode(', ', $allowed));
}
return $this->hasOne(OrderItem::class)->ofMany($column, strtoupper($aggregate)); Type guard
function isValidOfManyAggregate(string $agg): bool {
return in_array(strtolower($agg), ['min', 'max'], true);
} Prevention
- Remember ofMany only supports MIN and MAX.
- If you need AVG/SUM-style selection, build it as a manual join subquery outside ofMany.
- Validate aggregate input against ['min','max'] before passing through.
When it happens
Trigger: Calling $relation->ofMany('price', 'AVG') or ->ofMany(['price' => 'sum']). The lowercase check rejects anything not in ['min','max'].
Common situations: Assuming ofMany supports arbitrary SQL aggregates; copy-paste from a query builder that used sum/avg; wanting 'latest by highest sum' which the API does not provide.
Related errors
- Collection given to whereAttachedTo method may not be empty.
- Collection given to whereBelongsTo method may not be empty.
- The provided value may not be null.
- Add fillable property
- Call to undefined method
AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11).
Data as JSON: /api/errors/ecf27c1f4ef993f8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php:95
$keyName = $this->query->getModel()->getKeyName();
$columns = is_string($columns = $column) ? [
$column => $aggregate,
$keyName => $aggregate,
] : $column;
if (! array_key_exists($keyName, $columns)) {
$columns[$keyName] = 'MAX';
}
if ($aggregate instanceof Closure) {
$closure = $aggregate;
}
foreach ($columns as $column => $aggregate) {
if (! in_array(strtolower($aggregate), ['min', 'max'])) {
throw new InvalidArgumentException("Invalid aggregate [{$aggregate}] used within ofMany relation. Available aggregates: MIN, MAX");
}
$subQuery = $this->newOneOfManySubQuery(
$this->getOneOfManySubQuerySelectColumns(),
array_merge([$column], $previous['columns'] ?? []),
$aggregate,
);
if (isset($previous)) {
$this->addOneOfManyJoinSubQuery(
$subQuery,
$previous['subQuery'],
$previous['columns'],
);
}
if (isset($closure)) {
$closure($subQuery);View on GitHub (pinned to e0f6eb3518)