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

  1. Use only 'MIN' or 'MAX' (case-insensitive) as the aggregate.
  2. If you need AVG/SUM-style selection, write it as a custom relationship query or a join subquery outside ofMany.
  3. 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

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


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)