{"record":{"id":"ecf27c1f4ef993f8","repo":"laravel/framework","slug":"invalid-aggregate-aggregate-used-within-ofman","errorCode":null,"errorMessage":"Invalid aggregate [{$aggregate}] used within ofMany relation. Available aggregates: MIN, MAX","messagePattern":"Invalid aggregate \\[(.+?)\\] used within ofMany relation\\. Available aggregates: MIN, MAX","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php","lineNumber":95,"sourceCode":"\n        $keyName = $this->query->getModel()->getKeyName();\n\n        $columns = is_string($columns = $column) ? [\n            $column => $aggregate,\n            $keyName => $aggregate,\n        ] : $column;\n\n        if (! array_key_exists($keyName, $columns)) {\n            $columns[$keyName] = 'MAX';\n        }\n\n        if ($aggregate instanceof Closure) {\n            $closure = $aggregate;\n        }\n\n        foreach ($columns as $column => $aggregate) {\n            if (! in_array(strtolower($aggregate), ['min', 'max'])) {\n                throw new InvalidArgumentException(\"Invalid aggregate [{$aggregate}] used within ofMany relation. Available aggregates: MIN, MAX\");\n            }\n\n            $subQuery = $this->newOneOfManySubQuery(\n                $this->getOneOfManySubQuerySelectColumns(),\n                array_merge([$column], $previous['columns'] ?? []),\n                $aggregate,\n            );\n\n            if (isset($previous)) {\n                $this->addOneOfManyJoinSubQuery(\n                    $subQuery,\n                    $previous['subQuery'],\n                    $previous['columns'],\n                );\n            }\n\n            if (isset($closure)) {\n                $closure($subQuery);","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php#L77-L113","documentation":"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.","triggerScenarios":"Calling $relation->ofMany('price', 'AVG') or ->ofMany(['price' => 'sum']). The lowercase check rejects anything not in ['min','max'].","commonSituations":"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.","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)."],"exampleFix":"// before\nreturn $this->hasOne(OrderItem::class)->ofMany('price', 'AVG'); // throws\n\n// after\nreturn $this->hasOne(OrderItem::class)->ofMany('price', 'MAX'); // most expensive item\n// or 'MIN' for cheapest","handlingStrategy":"validation","validationCode":"$allowed = ['min', 'max'];\nif (! in_array(strtolower($aggregate), $allowed, true)) {\n    throw new \\InvalidArgumentException(\"ofMany aggregate must be one of: \" . implode(', ', $allowed));\n}\nreturn $this->hasOne(OrderItem::class)->ofMany($column, strtoupper($aggregate));","typeGuard":"function isValidOfManyAggregate(string $agg): bool {\n    return in_array(strtolower($agg), ['min', 'max'], true);\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["eloquent","relationships","one-of-many","aggregate","validation"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}