Blame | Letzte Änderung | Log anzeigen | RSS feed
# Laravel Date Scopes[](https://packagist.org/packages/laracraft-tech/laravel-date-scopes)[](https://github.com/laracraft-tech/laravel-date-scopes/actions/workflows/run-tests.yml)[](https://packagist.org/packages/laracraft-tech/laravel-date-scopes)<!--[](https://packagist.org/packages/laracraft-tech/laravel-date-scopes)-->The package provides a big range of useful date scopes for your Laravel Eloquent models!## InstallationYou can install the package via composer:```bashcomposer require laracraft-tech/laravel-date-scopes```## Configuration### Inclusive/ExclusiveIn **statistics**, when asking for "the last 7 days", the current day may or may not be includedin the calculation depending on the context and the specific requirements of the analysis.If you want to **include** the current day in the calculation, you would generally use an **inclusive** range,meaning that you would include records created on the **current day** as well as recordscreated in the previous 6 days.If you want to **exclude** the current day in the calculation, you would generally use an **exclusive** range,meaning that you would include records created in the previous 7 days,but not records created on the **current day**.Ultimately, it **depends** on the context and what you're trying to achieve with your data.It's always a good idea to clarify the requirements and expectations with stakeholdersto ensure that you're including or excluding the correct records.The same **concept** applies to other time intervals like weeks, months, quarters, and years etc.The default for this package is **exclusive** approach,which means when you for instance query for the last 7 days it will **not include** the current day!You can change the default if you need in the published config file.### Global configurationYou can publish the config file with:```bashphp artisan vendor:publish --tag="date-scopes-config"```This is the contents of the published config file:```phpreturn [/*** If you want to include the current day/week/month/year etc. in the range,* you could use the inclusive range here as a default.* Note that you can also fluently specify the range for quite every scope we offer* directly when using the scope:* Transaction::ofLast7Days(DateRange::INCLUSIVE); (this works for all but the singular "ofLast"-scopes)* This will do an inclusive query, even though the global default range here is set to exclusive.*/'default_range' => env('DATE_SCOPES_DEFAULT_RANGE', DateRange::EXCLUSIVE->value),/*** If you use a global custom created_at column name, change it here.*/'created_column' => env('DATE_SCOPES_CREATED_COLUMN', 'created_at'),];```If you want to change the default range to inclusive set `DATE_SCOPES_DEFAULT_RANGE=inclusive` in your `.env`.### Fluent configurationAs already mentioned above in the `default_range` config description text,you can also fluently specify the range for quite every scope we offerdirectly when using the scope:```php// This works for all "ofLast"-scopes, expect the singulars like "ofLastHour",// because it would not make sense for those.Transaction::ofLast7Days(DateRange::INCLUSIVE);```This will do an inclusive query (today-6 days), even though the global default range here was set to exclusive.## Scopes- [`seconds`](#seconds)- [`minutes`](#minutes)- [`hours`](#hours)- [`days`](#days)- [`weeks`](#weeks)- [`months`](#months)- [`quarters`](#quarters)- [`years`](#years)- [`decades`](#decades)- [`centuries`](#centuries)- [`millenniums`](#millenniums)- [`toNow/toDate`](#toNowtoDate)Let's assume you have an `Transaction` model class.Now when you give it the `DateScopes` trait, you can use the following scopes:```phpuse LaracraftTech\LaravelDateScopes\DateScopes;class Transaction extends Model{use DateScopes;}```### Seconds```php// query by SECONDSTransaction::ofJustNow(); // query transactions created just nowTransaction::ofLastSecond(); // query transactions created during the last secondTransaction::ofLast15Seconds(); // query transactions created during the last 15 secondsTransaction::ofLast30Seconds(); // query transactions created during the last 30 secondsTransaction::ofLast45Seconds(); // query transactions created during the last 45 secondsTransaction::ofLast60Seconds(); // query transactions created during the last 60 secondsTransaction::ofLastSeconds(120); // query transactions created during the last N seconds```### Minutes```php// query by MINUTESTransaction::ofLastMinute(); // query transactions created during the last minuteTransaction::ofLast15Minutes(); // query transactions created during the last 15 minutesTransaction::ofLast30Minutes(); // query transactions created during the last 30 minutesTransaction::ofLast45Minutes(); // query transactions created during the last 45 minutesTransaction::ofLast60Minutes(); // query transactions created during the last 60 minutesTransaction::ofLastMinutes(120); // query transactions created during the last N minutes```### Hours```php// query by HOURSTransaction::ofLastHour(); // query transactions created during the last hourTransaction::ofLast6Hours(); // query transactions created during the last 6 hoursTransaction::ofLast12Hours(); // query transactions created during the last 12 hoursTransaction::ofLast18Hours(); // query transactions created during the last 18 hoursTransaction::ofLast24Hours(); // query transactions created during the last 24 hoursTransaction::ofLastHours(48); // query transactions created during the last N hours```### Days```php// query by DAYSTransaction::ofToday(); // query transactions created todayTransaction::ofYesterday(); // query transactions created yesterdayTransaction::ofLast7Days(); // query transactions created during the last 7 daysTransaction::ofLast21Days(); // query transactions created during the last 21 daysTransaction::ofLast30Days(); // query transactions created during the last 30 daysTransaction::ofLastDays(60); // query transactions created during the last N days```### Weeks```php// query by WEEKSTransaction::ofLastWeek(); // query transactions created during the last weekTransaction::ofLast2Weeks(); // query transactions created during the last 2 weeksTransaction::ofLast3Weeks(); // query transactions created during the last 3 weeksTransaction::ofLast4Weeks(); // query transactions created during the last 4 weeksTransaction::ofLastWeeks(8); // query transactions created during the last N weeks```### Months```php// query by MONTHSTransaction::ofLastMonth(); // query transactions created during the last monthTransaction::ofLast3Months(); // query transactions created during the last 3 monthsTransaction::ofLast6Months(); // query transactions created during the last 6 monthsTransaction::ofLast9Months(); // query transactions created during the last 9 monthsTransaction::ofLast12Months(); // query transactions created during the last 12 monthsTransaction::ofLastMonths(24); // query transactions created during the last N months```### Quarters```php// query by QUARTERSTransaction::ofLastQuarter(); // query transactions created during the last quarterTransaction::ofLast2Quarters(); // query transactions created during the last 2 quartersTransaction::ofLast3Quarters(); // query transactions created during the last 3 quartersTransaction::ofLast4Quarters(); // query transactions created during the last 4 quartersTransaction::ofLastQuarters(8); // query transactions created during the last N quarters```### Years```php// query by YEARSTransaction::ofLastYear(); // query transactions created during the last yearTransaction::ofLastYears(2); // query transactions created during the last N years```### Decades```php// query by DECADESTransaction::ofLastDecade(); // query transactions created during the last decadeTransaction::ofLastDecades(2); // query transactions created during the last N decades```### CenturiesThe **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.Maybe you would expect a range from: 1900-01-01 00:00:00 to 1999-12-31 23:59:59.Checkout Wikipedia for this behavior: https://en.wikipedia.org/wiki/20th_century```php// query by CENTURIESTransaction::ofLastCentury(); // query transactions created during the last centuryTransaction::ofLastCenturies(2); // query transactions created during the last N centuries```### MillenniumsThe **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.Maybe you would expect a range from: 1000-01-01 00:00:00 to 1999-12-31 23:59:59.Checkout Wikipedia for this behavior: https://en.wikipedia.org/wiki/2nd_millennium```php// query by MILLENNIUMSTransaction::ofLastMillennium(); // query transactions created during the last millenniumTransaction::ofLastMillenniums(2); // query transactions created during the last N millenniums```### toNow/toDate```php// query by toNow/toDateTransaction::secondToNow(); // query transactions created during the start of the current second till now (equivalent of just now)Transaction::minuteToNow(); // query transactions created during the start of the current minute till nowTransaction::hourToNow(); // query transactions created during the start of the current hour till nowTransaction::dayToNow(); // query transactions created during the start of the current day till nowTransaction::weekToDate(); // query transactions created during the start of the current week till nowTransaction::monthToDate(); // query transactions created during the start of the current month till nowTransaction::quarterToDate(); // query transactions created during the start of the current quarter till nowTransaction::yearToDate(); // query transactions created during the start of the current year till nowTransaction::decadeToDate(); // query transactions created during the start of the current decade till nowTransaction::centuryToDate(); // query transactions created during the start of the current century till nowTransaction::millenniumToDate(); // query transactions created during the start of the current millennium till now```## Testing```bashcomposer test```## ChangelogPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.## ContributingPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.## Security VulnerabilitiesPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.## Credits- [Zacharias Creutznacher](https://github.com/laracraft-tech)- [All Contributors](../../contributors)## LicenseThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.