Skip to content

Handle incompatible aggregate function and column in @orderBy directive #2487

Open
@jdjoshuadavison

Description

@jdjoshuadavison

What problem does this feature proposal attempt to solve?

The orderBy directive provides 5 aggregate functions: AVG, MIN, MAX, SUM, COUNT. Not all of these functions can be used with all column types. This should be handled and should return a user friendly message in the response.

Specifically, AVG and SUM only work on numeric column types.

Which possible solutions should be considered?

Handling the specific issue mentioned with AVG and SUM on non-numeric types, could be solved simply with:

$relationModel = $builder->getModel()->$relation()->getRelated()->whereNotNull($relationColumn)->first();

if (! is_numeric($relationModel->$relationColumn) && in_array($aggregate, ['sum', 'avg'])) {
    throw new Error("$relation cannot be aggregated by SUM or AVG, please choose a different aggregate and try again.");
}

However, a more robust way of testing whether the aggregate function actually works with the specified column would be better.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementA feature or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions