groupBy, DB::raw("{$function}({$this->getWrappedColumn()}) as aggregate"), ...$this->additionalColumns, ]; $results = $this->query ->select($select) ->groupBy($this->groupBy, ...$this->additionalColumns) ->when( $this->dateRange, fn($query) => $query->whereBetween($this->dateColumn, [ $this->dateRange->start, $this->dateRange->end, ]), ) ->limit($this->limit) ->get(); $data = $results->map(function ($result) { $finalResult = [ 'label' => $this->getLabel($result), 'value' => $this->round($result->aggregate), ]; foreach ($this->additionalColumns as $column) { $finalResult[$column] = $result->{$column}; } return $finalResult; }); $total = $data->sum('value'); $data = $data ->map(function ($item) use ($total) { $item['percentage'] = round((100 * $item['value']) / $total, 1); return $item; }) ->sortByDesc('value') ->values(); return $data->all(); } protected function getLabel(Model $result): string { $label = with( $result->{last(explode('.', $this->groupBy))}, fn($key) => $key, ); return __(ucfirst($label)); } }