laravel-trend icon indicating copy to clipboard operation
laravel-trend copied to clipboard

Update how the date records are queried based on the start and end dates provided

Open 1jason1 opened this issue 2 years ago • 3 comments

My appologies wasnt able to create a pull request.

This is just an update to Trend.php on how the date records are queried based on the start and end dates provided.

Originally if doing a count by month, day, or year the "start" and "end" dates are Carbon instances and would include the time in the query which was excluding some records. The solution I came up with is based on the aggregation which formats the Carbon instances to better align with the periods being queried. There may be another solution but this worked for me.

image

1jason1 avatar Mar 08 '23 19:03 1jason1

Had the same issue and this fixed it for me.

BuggerSee avatar Dec 01 '23 01:12 BuggerSee

Had the same issue... thanks @1jason1 Here is if anyone to use it as own/custom Library to extend the Trend class.

<?php
namespace App\Lib;

use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

class Trend extends \Flowframe\Trend\Trend {

	public function queryDateFormat (Carbon $date): string | Carbon
	{
		return match ($this->interval) {
			'day', 'month', 'year' => $date->toDateString(), 
			default => $date,
		};
	}
	public function aggregate(string $column, string $aggregate): Collection
	{
		$values = $this->builder
			->toBase()
			->selectRaw("
				{$this->getSqlDate()} as {$this->dateAlias},
				{$aggregate}({$column}) as aggregate
			")
			->where(function ($q) {
				$q->whereBetween($this->dateColumn, [
					$this->queryDateformat($this->start), 
					$this->queryDateformat($this->end)
				]);
			})
			->groupBy($this->dateAlias)
			->orderBy($this->dateAlias)
			->get();

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

}

jinbatsu avatar Feb 18 '24 20:02 jinbatsu

I had the same issue with a date field retruning incorrect trend values. This fixed it for me and should be implemented.

useahawk avatar Sep 19 '24 12:09 useahawk