Skip to content

Commit b3c6353

Browse files
committed
Add Query Builder support
Add custom table definition support Remove eloquent toBase() method to support Query Builder Update README.md
1 parent b296a64 commit b3c6353

File tree

3 files changed

+113
-109
lines changed

3 files changed

+113
-109
lines changed

README.md

+82-81
Original file line numberDiff line numberDiff line change
@@ -13,112 +13,130 @@ composer require eliseekn/laravel-metrics
1313
## Features
1414
- MySQL support
1515
- Verbose query builder
16-
- Custom columns definition support
17-
- Days and months names translation with Carbon
16+
- Custom columns and table definition
17+
- Days and months translation with Carbon
1818

1919
## Usage
20+
21+
### With Eloquent Query
22+
2023
Import the `Eliseekn\LaravelMetrics\LaravelMetrics` class in your controller and use it as follows :
2124

2225
```php
23-
// generate trends of the sum of the orders amount for the current year
24-
LaravelMetrics::query(Order::query())
25-
->sum('amount')
26+
// generate trends of products amount's sum for the current year
27+
LaravelMetrics::query(Product::query())
28+
->count()
2629
->byMonth()
2730
->trends();
2831

29-
// generate trends of the sum of the orders amount for the last 6 months of the current year including the current month
32+
// generate trends of orders amount's sum for the last 6 months of the current year including current month
3033
LaravelMetrics::query(Order::query())
3134
->sum('amount')
3235
->byMonth(6)
3336
->trends();
3437

35-
// generate trends of count of the products for the last 3 years including the current year
36-
LaravelMetrics::query(Product::query())
37-
->count()
38-
->byYear(3)
39-
->trends();
40-
41-
// generate total sum of the orders amount for every year
38+
// generate total orders amount's sum
4239
LaravelMetrics::query(Order::query())
4340
->sum('amount')
4441
->byYear()
4542
->metrics();
4643

47-
// generate total count of the product for the current day of the current month
44+
// generate total product count for the current day
4845
LaravelMetrics::query(Product::query())
4946
->count()
5047
->byDay(1)
5148
->metrics();
52-
53-
// generate total count of the product for the current month
54-
LaravelMetrics::query(Product::query())
55-
->count()
56-
->byWeek()
57-
->metrics();
58-
59-
// generate trends of count of posts for the current month
60-
// by using a custom query and a custom date column
49+
```
50+
51+
- Using custom query
52+
```php
6153
LaravelMetrics::query(
6254
Post::query()->where('user_id', auth()->id())
6355
)
56+
->count()
57+
->byDay()
58+
->trends();
59+
```
60+
61+
- Using custom date column
62+
```php
63+
LaravelMetrics::query(Post::query())
6464
->count()
6565
->byDay()
6666
->dateColumn('published_at')
6767
->trends();
68-
69-
// generate trends of count of posts for a range of dates
70-
LaravelMetrics::query(
71-
Post::query()->where('user_id', auth()->id())
72-
)
68+
```
69+
70+
- Using date range
71+
```php
72+
LaravelMetrics::query(Post::query()))
7373
->count()
7474
->between('2020-05-01', '2022-08-21')
7575
->trends();
76+
```
7677

77-
// generate total count of the orders for the current year
78-
// by using a custom label column
78+
- Using custom label column
79+
```php
7980
LaravelMetrics::query(Order::query())
8081
->count()
8182
->byMonth(12)
8283
->labelColumn('status')
8384
->trends();
8485
```
8586

86-
### Types of periods
87+
- Using custom table
88+
```php
89+
LaravelMetrics::query(
90+
Order::query()->join('orders', 'orders.id', 'users.order_id')
91+
)
92+
->count()
93+
->table('users')
94+
->labelColumn('name')
95+
->trends();
96+
```
97+
98+
### With Query Builder
8799
```php
88-
->byDay(int $count = 0)
89-
->byWeek(int $count = 0)
90-
->byMonth(int $count = 0)
91-
->byYear(int $count = 0)
92-
->between(string $startDate, string $endDate)
100+
LaravelMetrics::query(
101+
DB::table('orders')
102+
)
103+
->sum('amount')
104+
->byMonth()
105+
->trends();
93106
```
94107

108+
### With traits
109+
110+
Add `HasMetrics` trait to your models and use it as follows :
111+
95112
```php
96-
$count = 0 => for every day, week, month or year
97-
$count = 1 => for the current day, week, month or year
98-
$count > 1 => for an interval of day, week, month or year from the $count value to now
99-
$period = 'day', 'week', 'month' or 'year'
113+
Order::metrics()
114+
->sum('amount')
115+
->byMonth()
116+
->trends();
100117
```
101118

102-
#### Notes
103-
Periods are typically defined for the current day, week, month or year. However, you can define a specific value using dedicated methods. For example:
119+
### Types of periods
104120
```php
105-
// generate total count of the orders for the year 2023
106-
//// by using a custom label column
121+
byDay(int $count = 0)
122+
byWeek(int $count = 0)
123+
byMonth(int $count = 0)
124+
byYear(int $count = 0)
125+
between(string $startDate, string $endDate)
126+
```
127+
128+
**Note :** Periods are typically defined for the current day, week, month or year. However, you can define a specific value using dedicated methods. For example:
129+
130+
```php
131+
// generate trends of orders count for the year 2023
107132
LaravelMetrics::query(Order::query())
108133
->count()
109134
->byMonth(12)
110135
->forYear(2023)
111136
->labelColumn('status')
112137
->trends();
113138

114-
// generate total count of the product for the current day of the month february
115-
LaravelMetrics::query(Product::query())
116-
->count()
117-
->byDay(1)
118-
->forMonth(2)
119-
->metrics();
120-
121-
// generate total sum of the orders amount for the month march only
139+
// generate total orders amount's sum for the third month only
122140
LaravelMetrics::query(Product::query())
123141
->sum('amount')
124142
->byMonth(1)
@@ -127,44 +145,27 @@ LaravelMetrics::query(Product::query())
127145
```
128146

129147
```php
130-
->forDay(int $day)
131-
->forWeek(int $week)
132-
->forMonth(int $month)
133-
->forYear(int $year)
148+
forDay(int $day)
149+
forWeek(int $week)
150+
forMonth(int $month)
151+
forYear(int $year)
134152
```
135153

136-
137154
### Types of aggregates
138155
```php
139-
->count(string $column = 'id')
140-
->average(string $column)
141-
->sum(string $column)
142-
->max(string $column)
143-
->min(string $column)
156+
count(string $column = 'id')
157+
average(string $column)
158+
sum(string $column)
159+
max(string $column)
160+
min(string $column)
144161
```
145162

146163
### Types of data
147164
```php
148-
->trends() // retrieves trends values for charts
149-
->metrics() // retrieves total values
165+
trends() // retrieves trends values for charts
166+
metrics() // retrieves total value
150167
```
151168

152-
### Traits
153-
154-
Add `HasMetrics` trait to your models and use it as follows :
155-
```php
156-
// generate trends of the sum of the orders amount for the current year
157-
Order::metrics()
158-
->sum('amount')
159-
->byMonth()
160-
->trends();
161-
162-
// generate total count of the product for the current month
163-
Product::metrics()
164-
->count()
165-
->byWeek()
166-
->metrics();
167-
```
168169
## Translations
169170

170171
Days and months names are automatically translated using `config(app.locale)` except 'week' period.
@@ -177,7 +178,7 @@ Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recen
177178

178179
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
179180

180-
### Security
181+
## Security
181182

182183
If you discover any security related issues, please email `[email protected]` instead of using the issue tracker.
183184

src/HasMetrics.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Eliseekn\LaravelMetrics;
45

0 commit comments

Comments
 (0)