From 4c7d5d218641042d6663ed2065db6bcf5f973a76 Mon Sep 17 00:00:00 2001 From: eliseekn Date: Fri, 25 Aug 2023 12:55:16 +0000 Subject: [PATCH] Add forMonth and forYear methods Update README.md --- README.md | 32 ++++++++++++++++++++++++++++++-- src/LaravelMetrics.php | 14 ++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 54c122b..e4afc2b 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ LaravelMetrics::query(Order::query()) ->byYear() ->metrics(); -// generate total count of the product for the current day of the current week +// generate total count of the product for the current day of the current month LaravelMetrics::query(Product::query()) ->count() ->byDay(1) @@ -56,7 +56,7 @@ LaravelMetrics::query(Product::query()) ->byWeek() ->metrics(); -// generate trends of count of posts for the current week +// generate trends of count of posts for the current month // by using a custom query and a custom date column LaravelMetrics::query( Post::query()->where('user_id', auth()->id()) @@ -74,6 +74,14 @@ LaravelMetrics::query( ->between('2020-05-01', '2022-08-21') ->trends(); +// generate total count of the orders for the current year +// by using a custom label column +LaravelMetrics::query(Order::query()) + ->count() + ->byMonth(12) + ->labelColumn('status') + ->trends(); + // generate total count of the orders for the current year // by using a custom label column LaravelMetrics::query(Order::query()) @@ -100,6 +108,26 @@ $count > 1 => for an interval of day, week, month or year from the $count value $period = 'day', 'week', 'month' or 'year' ``` +#### Notes +Periods are typically defined for the current month and/or year. However, you also have the option to define a specific month or year using dedicated methods. For example: +```php +// generate total count of the orders for the year 2023 +//// by using a custom label column +LaravelMetrics::query(Order::query()) + ->count() + ->byMonth(12) + ->forYear(2023) + ->labelColumn('status') + ->trends(); + +// generate total count of the product for the current day of the month february +LaravelMetrics::query(Product::query()) + ->count() + ->byDay(1) + ->forMonth(2) + ->metrics(); +``` + ### Types of aggregates ```php ->count(string $column = 'id') diff --git a/src/LaravelMetrics.php b/src/LaravelMetrics.php index 51ae7b1..c40d6b9 100644 --- a/src/LaravelMetrics.php +++ b/src/LaravelMetrics.php @@ -92,6 +92,18 @@ public function between(string $start, string $end): self return $this; } + public function forMonth(int $month): self + { + $this->month = $month; + return $this; + } + + public function forYear(int $year): self + { + $this->year = $year; + return $this; + } + public function aggregate(string $aggregate, string $column): self { $aggregate = strtolower($aggregate); @@ -158,7 +170,6 @@ protected function metricsData(): mixed ->selectRaw($this->asData("$this->aggregate($this->column)")) ->whereYear($this->dateColumn, $this->year) ->whereMonth($this->dateColumn, $this->month) - ->where(DB::raw($this->formatPeriod(Period::WEEK->value)), $this->week) ->when($this->count === 1, function (QueryBuilder $query) { return $query->where(DB::raw("day($this->dateColumn)"), $this->day); }) @@ -233,7 +244,6 @@ protected function trendsData(): Collection ->selectRaw($this->asData("$this->aggregate($this->column)") . ", " . $this->asLabel(Period::DAY->value)) ->whereYear($this->dateColumn, $this->year) ->whereMonth($this->dateColumn, $this->month) - ->where(DB::raw($this->formatPeriod(Period::WEEK->value)), $this->week) ->when($this->count === 1, function (QueryBuilder $query) { return $query->where(DB::raw("day($this->dateColumn)"), $this->day); })