Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1jason1 opened this issue Mar 8, 2023 · 2 comments

Comments

@1jason1
Copy link

1jason1 commented Mar 8, 2023

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

@BuggerSee
Copy link

Had the same issue and this fixed it for me.

@jinbatsu
Copy link

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);
	}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants