Flowframe / laravel-trend

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

PgsqlAdapter and CarbonDateFormat date format mismatch #62

Open alexsmirnovdotcom opened 4 months ago

alexsmirnovdotcom commented 4 months ago

PgsqlAdapter for query in 'hour' interval get 'YYYY-MM-DD HH24:00:00' format. But CarbonDateFormat for 'hour' interval get 'Y-m-d H:00'. This is get a duplicated empty TrendValue`s because from database receives with seconds, and generate stubs without.

neutronstein commented 1 month ago

This issue can be easily resolved by extending Trend class, overriding getCarbonDateFormat function and using the new class instead.

class TrendX extends \Flowframe\Trend\Trend
{
    protected function getCarbonDateFormat(): string
    {
        if($this->interval == 'hour') {
           return 'Y-m-d H:00:00';
        }
        return parent::getCarbonDateFormat();
    }
}

// Usage
TrendX::model(User::class)->between(...)->perHour()->count()