| 1675 |
lars |
1 |
# Laravel Date Scopes
|
|
|
2 |
|
|
|
3 |
[](https://packagist.org/packages/laracraft-tech/laravel-date-scopes)
|
|
|
4 |
[](https://github.com/laracraft-tech/laravel-date-scopes/actions/workflows/run-tests.yml)
|
|
|
5 |
[](https://packagist.org/packages/laracraft-tech/laravel-date-scopes)
|
|
|
6 |
<!--[](https://packagist.org/packages/laracraft-tech/laravel-date-scopes)-->
|
|
|
7 |
|
|
|
8 |
The package provides a big range of useful date scopes for your Laravel Eloquent models!
|
|
|
9 |
|
|
|
10 |
## Installation
|
|
|
11 |
|
|
|
12 |
You can install the package via composer:
|
|
|
13 |
|
|
|
14 |
```bash
|
|
|
15 |
composer require laracraft-tech/laravel-date-scopes
|
|
|
16 |
```
|
|
|
17 |
|
|
|
18 |
## Configuration
|
|
|
19 |
|
|
|
20 |
### Inclusive/Exclusive
|
|
|
21 |
|
|
|
22 |
In **statistics**, when asking for "the last 7 days", the current day may or may not be included
|
|
|
23 |
in the calculation depending on the context and the specific requirements of the analysis.
|
|
|
24 |
|
|
|
25 |
If you want to **include** the current day in the calculation, you would generally use an **inclusive** range,
|
|
|
26 |
meaning that you would include records created on the **current day** as well as records
|
|
|
27 |
created in the previous 6 days.
|
|
|
28 |
|
|
|
29 |
If you want to **exclude** the current day in the calculation, you would generally use an **exclusive** range,
|
|
|
30 |
meaning that you would include records created in the previous 7 days,
|
|
|
31 |
but not records created on the **current day**.
|
|
|
32 |
|
|
|
33 |
Ultimately, it **depends** on the context and what you're trying to achieve with your data.
|
|
|
34 |
It's always a good idea to clarify the requirements and expectations with stakeholders
|
|
|
35 |
to ensure that you're including or excluding the correct records.
|
|
|
36 |
|
|
|
37 |
The same **concept** applies to other time intervals like weeks, months, quarters, and years etc.
|
|
|
38 |
|
|
|
39 |
The default for this package is **exclusive** approach,
|
|
|
40 |
which means when you for instance query for the last 7 days it will **not include** the current day!
|
|
|
41 |
You can change the default if you need in the published config file.
|
|
|
42 |
|
|
|
43 |
### Global configuration
|
|
|
44 |
|
|
|
45 |
You can publish the config file with:
|
|
|
46 |
|
|
|
47 |
```bash
|
|
|
48 |
php artisan vendor:publish --tag="date-scopes-config"
|
|
|
49 |
```
|
|
|
50 |
|
|
|
51 |
This is the contents of the published config file:
|
|
|
52 |
|
|
|
53 |
```php
|
|
|
54 |
return [
|
|
|
55 |
/**
|
|
|
56 |
* If you want to include the current day/week/month/year etc. in the range,
|
|
|
57 |
* you could use the inclusive range here as a default.
|
|
|
58 |
* Note that you can also fluently specify the range for quite every scope we offer
|
|
|
59 |
* directly when using the scope:
|
|
|
60 |
* Transaction::ofLast7Days(DateRange::INCLUSIVE); (this works for all but the singular "ofLast"-scopes)
|
|
|
61 |
* This will do an inclusive query, even though the global default range here is set to exclusive.
|
|
|
62 |
*/
|
|
|
63 |
'default_range' => env('DATE_SCOPES_DEFAULT_RANGE', DateRange::EXCLUSIVE->value),
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* If you use a global custom created_at column name, change it here.
|
|
|
67 |
*/
|
|
|
68 |
'created_column' => env('DATE_SCOPES_CREATED_COLUMN', 'created_at'),
|
|
|
69 |
];
|
|
|
70 |
```
|
|
|
71 |
|
|
|
72 |
If you want to change the default range to inclusive set `DATE_SCOPES_DEFAULT_RANGE=inclusive` in your `.env`.
|
|
|
73 |
|
|
|
74 |
### Fluent configuration
|
|
|
75 |
|
|
|
76 |
As already mentioned above in the `default_range` config description text,
|
|
|
77 |
you can also fluently specify the range for quite every scope we offer
|
|
|
78 |
directly when using the scope:
|
|
|
79 |
|
|
|
80 |
```php
|
|
|
81 |
// This works for all "ofLast"-scopes, expect the singulars like "ofLastHour",
|
|
|
82 |
// because it would not make sense for those.
|
|
|
83 |
Transaction::ofLast7Days(DateRange::INCLUSIVE);
|
|
|
84 |
```
|
|
|
85 |
|
|
|
86 |
This will do an inclusive query (today-6 days), even though the global default range here was set to exclusive.
|
|
|
87 |
|
|
|
88 |
## Scopes
|
|
|
89 |
|
|
|
90 |
- [`seconds`](#seconds)
|
|
|
91 |
- [`minutes`](#minutes)
|
|
|
92 |
- [`hours`](#hours)
|
|
|
93 |
- [`days`](#days)
|
|
|
94 |
- [`weeks`](#weeks)
|
|
|
95 |
- [`months`](#months)
|
|
|
96 |
- [`quarters`](#quarters)
|
|
|
97 |
- [`years`](#years)
|
|
|
98 |
- [`decades`](#decades)
|
|
|
99 |
- [`centuries`](#centuries)
|
|
|
100 |
- [`millenniums`](#millenniums)
|
|
|
101 |
- [`toNow/toDate`](#toNowtoDate)
|
|
|
102 |
|
|
|
103 |
Let's assume you have an `Transaction` model class.
|
|
|
104 |
Now when you give it the `DateScopes` trait, you can use the following scopes:
|
|
|
105 |
|
|
|
106 |
```php
|
|
|
107 |
use LaracraftTech\LaravelDateScopes\DateScopes;
|
|
|
108 |
|
|
|
109 |
class Transaction extends Model
|
|
|
110 |
{
|
|
|
111 |
use DateScopes;
|
|
|
112 |
}
|
|
|
113 |
```
|
|
|
114 |
|
|
|
115 |
### Seconds
|
|
|
116 |
|
|
|
117 |
```php
|
|
|
118 |
// query by SECONDS
|
|
|
119 |
Transaction::ofJustNow(); // query transactions created just now
|
|
|
120 |
Transaction::ofLastSecond(); // query transactions created during the last second
|
|
|
121 |
Transaction::ofLast15Seconds(); // query transactions created during the last 15 seconds
|
|
|
122 |
Transaction::ofLast30Seconds(); // query transactions created during the last 30 seconds
|
|
|
123 |
Transaction::ofLast45Seconds(); // query transactions created during the last 45 seconds
|
|
|
124 |
Transaction::ofLast60Seconds(); // query transactions created during the last 60 seconds
|
|
|
125 |
Transaction::ofLastSeconds(120); // query transactions created during the last N seconds
|
|
|
126 |
```
|
|
|
127 |
|
|
|
128 |
### Minutes
|
|
|
129 |
|
|
|
130 |
```php
|
|
|
131 |
// query by MINUTES
|
|
|
132 |
Transaction::ofLastMinute(); // query transactions created during the last minute
|
|
|
133 |
Transaction::ofLast15Minutes(); // query transactions created during the last 15 minutes
|
|
|
134 |
Transaction::ofLast30Minutes(); // query transactions created during the last 30 minutes
|
|
|
135 |
Transaction::ofLast45Minutes(); // query transactions created during the last 45 minutes
|
|
|
136 |
Transaction::ofLast60Minutes(); // query transactions created during the last 60 minutes
|
|
|
137 |
Transaction::ofLastMinutes(120); // query transactions created during the last N minutes
|
|
|
138 |
```
|
|
|
139 |
|
|
|
140 |
### Hours
|
|
|
141 |
|
|
|
142 |
```php
|
|
|
143 |
// query by HOURS
|
|
|
144 |
Transaction::ofLastHour(); // query transactions created during the last hour
|
|
|
145 |
Transaction::ofLast6Hours(); // query transactions created during the last 6 hours
|
|
|
146 |
Transaction::ofLast12Hours(); // query transactions created during the last 12 hours
|
|
|
147 |
Transaction::ofLast18Hours(); // query transactions created during the last 18 hours
|
|
|
148 |
Transaction::ofLast24Hours(); // query transactions created during the last 24 hours
|
|
|
149 |
Transaction::ofLastHours(48); // query transactions created during the last N hours
|
|
|
150 |
```
|
|
|
151 |
|
|
|
152 |
### Days
|
|
|
153 |
|
|
|
154 |
```php
|
|
|
155 |
// query by DAYS
|
|
|
156 |
Transaction::ofToday(); // query transactions created today
|
|
|
157 |
Transaction::ofYesterday(); // query transactions created yesterday
|
|
|
158 |
Transaction::ofLast7Days(); // query transactions created during the last 7 days
|
|
|
159 |
Transaction::ofLast21Days(); // query transactions created during the last 21 days
|
|
|
160 |
Transaction::ofLast30Days(); // query transactions created during the last 30 days
|
|
|
161 |
Transaction::ofLastDays(60); // query transactions created during the last N days
|
|
|
162 |
```
|
|
|
163 |
|
|
|
164 |
### Weeks
|
|
|
165 |
|
|
|
166 |
```php
|
|
|
167 |
// query by WEEKS
|
|
|
168 |
Transaction::ofLastWeek(); // query transactions created during the last week
|
|
|
169 |
Transaction::ofLast2Weeks(); // query transactions created during the last 2 weeks
|
|
|
170 |
Transaction::ofLast3Weeks(); // query transactions created during the last 3 weeks
|
|
|
171 |
Transaction::ofLast4Weeks(); // query transactions created during the last 4 weeks
|
|
|
172 |
Transaction::ofLastWeeks(8); // query transactions created during the last N weeks
|
|
|
173 |
```
|
|
|
174 |
|
|
|
175 |
### Months
|
|
|
176 |
|
|
|
177 |
```php
|
|
|
178 |
// query by MONTHS
|
|
|
179 |
Transaction::ofLastMonth(); // query transactions created during the last month
|
|
|
180 |
Transaction::ofLast3Months(); // query transactions created during the last 3 months
|
|
|
181 |
Transaction::ofLast6Months(); // query transactions created during the last 6 months
|
|
|
182 |
Transaction::ofLast9Months(); // query transactions created during the last 9 months
|
|
|
183 |
Transaction::ofLast12Months(); // query transactions created during the last 12 months
|
|
|
184 |
Transaction::ofLastMonths(24); // query transactions created during the last N months
|
|
|
185 |
```
|
|
|
186 |
|
|
|
187 |
### Quarters
|
|
|
188 |
|
|
|
189 |
```php
|
|
|
190 |
// query by QUARTERS
|
|
|
191 |
Transaction::ofLastQuarter(); // query transactions created during the last quarter
|
|
|
192 |
Transaction::ofLast2Quarters(); // query transactions created during the last 2 quarters
|
|
|
193 |
Transaction::ofLast3Quarters(); // query transactions created during the last 3 quarters
|
|
|
194 |
Transaction::ofLast4Quarters(); // query transactions created during the last 4 quarters
|
|
|
195 |
Transaction::ofLastQuarters(8); // query transactions created during the last N quarters
|
|
|
196 |
```
|
|
|
197 |
|
|
|
198 |
### Years
|
|
|
199 |
|
|
|
200 |
```php
|
|
|
201 |
// query by YEARS
|
|
|
202 |
Transaction::ofLastYear(); // query transactions created during the last year
|
|
|
203 |
Transaction::ofLastYears(2); // query transactions created during the last N years
|
|
|
204 |
```
|
|
|
205 |
|
|
|
206 |
### Decades
|
|
|
207 |
|
|
|
208 |
```php
|
|
|
209 |
// query by DECADES
|
|
|
210 |
Transaction::ofLastDecade(); // query transactions created during the last decade
|
|
|
211 |
Transaction::ofLastDecades(2); // query transactions created during the last N decades
|
|
|
212 |
```
|
|
|
213 |
|
|
|
214 |
### Centuries
|
|
|
215 |
|
|
|
216 |
The **centuries** may return a different range then you maybe would expect. For instance `Transaction::ofLastCentury()` would apply a range from 1901-01-01 00:00:00 to 2000-12-31 23:59:59.
|
|
|
217 |
Maybe you would expect a range from: 1900-01-01 00:00:00 to 1999-12-31 23:59:59.
|
|
|
218 |
|
|
|
219 |
Checkout Wikipedia for this behavior: https://en.wikipedia.org/wiki/20th_century
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
```php
|
|
|
223 |
// query by CENTURIES
|
|
|
224 |
Transaction::ofLastCentury(); // query transactions created during the last century
|
|
|
225 |
Transaction::ofLastCenturies(2); // query transactions created during the last N centuries
|
|
|
226 |
```
|
|
|
227 |
|
|
|
228 |
### Millenniums
|
|
|
229 |
|
|
|
230 |
The **millenniums** may return a different range then you maybe would expect. For instance `Transaction::ofLastMillennium()` would apply a range from 1001-01-01 00:00:00 to 2000-12-31 23:59:59.
|
|
|
231 |
Maybe you would expect a range from: 1000-01-01 00:00:00 to 1999-12-31 23:59:59.
|
|
|
232 |
|
|
|
233 |
Checkout Wikipedia for this behavior: https://en.wikipedia.org/wiki/2nd_millennium
|
|
|
234 |
|
|
|
235 |
```php
|
|
|
236 |
// query by MILLENNIUMS
|
|
|
237 |
Transaction::ofLastMillennium(); // query transactions created during the last millennium
|
|
|
238 |
Transaction::ofLastMillenniums(2); // query transactions created during the last N millenniums
|
|
|
239 |
```
|
|
|
240 |
|
|
|
241 |
### toNow/toDate
|
|
|
242 |
|
|
|
243 |
```php
|
|
|
244 |
// query by toNow/toDate
|
|
|
245 |
Transaction::secondToNow(); // query transactions created during the start of the current second till now (equivalent of just now)
|
|
|
246 |
Transaction::minuteToNow(); // query transactions created during the start of the current minute till now
|
|
|
247 |
Transaction::hourToNow(); // query transactions created during the start of the current hour till now
|
|
|
248 |
Transaction::dayToNow(); // query transactions created during the start of the current day till now
|
|
|
249 |
Transaction::weekToDate(); // query transactions created during the start of the current week till now
|
|
|
250 |
Transaction::monthToDate(); // query transactions created during the start of the current month till now
|
|
|
251 |
Transaction::quarterToDate(); // query transactions created during the start of the current quarter till now
|
|
|
252 |
Transaction::yearToDate(); // query transactions created during the start of the current year till now
|
|
|
253 |
Transaction::decadeToDate(); // query transactions created during the start of the current decade till now
|
|
|
254 |
Transaction::centuryToDate(); // query transactions created during the start of the current century till now
|
|
|
255 |
Transaction::millenniumToDate(); // query transactions created during the start of the current millennium till now
|
|
|
256 |
```
|
|
|
257 |
|
|
|
258 |
## Testing
|
|
|
259 |
|
|
|
260 |
```bash
|
|
|
261 |
composer test
|
|
|
262 |
```
|
|
|
263 |
|
|
|
264 |
## Changelog
|
|
|
265 |
|
|
|
266 |
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
|
|
|
267 |
|
|
|
268 |
## Contributing
|
|
|
269 |
|
|
|
270 |
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
|
|
|
271 |
|
|
|
272 |
## Security Vulnerabilities
|
|
|
273 |
|
|
|
274 |
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
|
|
|
275 |
|
|
|
276 |
## Credits
|
|
|
277 |
|
|
|
278 |
- [Zacharias Creutznacher](https://github.com/laracraft-tech)
|
|
|
279 |
- [All Contributors](../../contributors)
|
|
|
280 |
|
|
|
281 |
## License
|
|
|
282 |
|
|
|
283 |
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
|