Skip to content

aggregate() in metrics overwrite user-defined select() calls #3624

@rennokki

Description

@rennokki
  • Laravel Version: 8.x
  • Nova Version: 3.3.0
  • PHP Version: 8.0
  • Database Driver & Version: -
  • Operating System and Version: -
  • Browser type and version: -
  • Reproduction Repository: -

Description:

The aggregate-based functions in metrics (partitions, for example) do allow you to specify a Builder instance to alter the querying instead of passing the model class name.

The problem is that the underlying aggregate() function performs a ->select() call, and specifying additional fields via ->addSelect() get overwritten by it.

I can give a slightly similar situation:

public function calculate(NovaRequest $request)
{
    $query = Post::query()->addSelect(DB::raw('JSON_EXTRACT(meta, "$.views") AS total_views'));

    return $this->count($request, $query, 'total_views');
}

The ->aggregate() method from Partition calls ->select() instead of ->addSelect(), making the previous defined JSON_EXTRACT to get overwritten:

$results = $query->select( // This one
    $groupBy, DB::raw("{$function}({$wrappedColumn}) as aggregate")
)->groupBy($groupBy)->get();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions