Flowframe / laravel-trend

Generate trends for your models. Easily generate charts or reports.
MIT License
670 stars 70 forks source link

Postgress issue: created_at must appear in the GROUP BY #16

Closed obrunsmann closed 1 year ago

obrunsmann commented 2 years ago

Error

SQLSTATE[42803]: Grouping error: 7 ERROR: column "logs.created_at" must appear in the GROUP BY clause or be used in an aggregate function LINE 2: to_char(created_at, 'YYYY-MM-DD') as date, ^ (SQL: select to_char(created_at, 'YYYY-MM-DD') as date, count(*) as aggregate from "logs" where "created_at" between 2022-05-14 00:00:00 and 2022-08-14 00:00:00 group by "date" order by "date" desc, "date" asc) (View: /var/www/html/resources/views/vendor/filament/widgets/stats-overview-widget.blade.php)

For testing I changed aggregate function to

    public function aggregate(string $column, string $aggregate): Collection
    {
        $values = $this->builder
            ->toBase()
            ->selectRaw("
                {$this->getSqlDate()} as date,
                {$aggregate}({$column}) as aggregate
            ")
            ->whereBetween($this->dateColumn, [$this->start, $this->end])
            ->groupBy('created_at')
            ->orderBy('created_at')
            ->get();

        return $this->mapValuesToDates($values);
    }

Which then works. So it looks like the table alias producing issues here.