{"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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php#L77-L113","documentation":"ofMany() on a has-one/has-many relation selects a single representative row by an aggregate (MIN/MAX). The implementation only supports 'min' and 'max' because it builds a deterministic sub-query join; any other aggregate (SUM, AVG, COUNT) is rejected with InvalidArgumentException listing the allowed set.","triggerScenarios":"Calling ->ofMany('price', 'AVG') or ->ofMany('score', 'sum'), or passing an array of column=>aggregate with a non-min/max value.","commonSituations":"Wanting an 'average' or 'total' representative row (which ofMany cannot express); copy-paste from SQL with a SUM/AVG; typos or wrong-case aggregates (case is lowercased before check, so case isn't the issue).","solutions":["Use only 'MIN' or 'MAX' for ofMany (e.g. ->ofMany('price', 'MAX') for the most expensive).","For more complex selection, pass a closure to ofMany() and build the sub-query manually.","If you need an average/sum-based representative, compute it separately and select the matching row with a custom where/subquery."],"exampleFix":"// before\nreturn $this->hasOne(Order::class)->ofMany('total', 'AVG');\n\n// after\nreturn $this->hasOne(Order::class)->ofMany('total', 'MAX');","handlingStrategy":"type-guard","validationCode":"$aggregate = strtoupper($aggregate);\nif (! in_array($aggregate, ['MIN', 'MAX'], true)) {\n    throw new \\InvalidArgumentException(\"ofMany supports only MIN/MAX, got {$aggregate}\");\n}\n$relation->ofMany($column, $aggregate);","typeGuard":"function isValidOfManyAggregate(string $aggregate): bool {\n    return in_array(strtoupper($aggregate), ['MIN', 'MAX'], true);\n}","tryCatchPattern":null,"preventionTips":["Restrict ofMany aggregates to MIN/MAX.","Use a closure for complex ofMany selection logic.","Compute AVG/SUM representatives separately and select the matching row."],"tags":["eloquent","relationships","has-one","aggregate","of-many"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}