Skip to content

Commit

Permalink
v2 - Add startFrom parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairahcaz committed Oct 12, 2023
1 parent 9b3c104 commit 4229cae
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 110 deletions.
68 changes: 49 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,36 @@
[![License](https://img.shields.io/packagist/l/laracraft-tech/laravel-date-scopes.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/laravel-date-scopes)
[![Total Downloads](https://img.shields.io/packagist/dt/laracraft-tech/laravel-date-scopes.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/laravel-date-scopes)

The package provides a big range of useful date scopes for your Laravel Eloquent models!
This package provides a big range of useful **date scopes** for your Laravel Eloquent models!

Let's assume you have a `Transaction` model.
If you now give it the `DateScopes` trait, you can do something like this:

```php
use LaracraftTech\LaravelDateScopes\DateScopes;

class Transaction extends Model
{
use DateScopes;
}

Transaction::ofToday(); // query transactions created today
Transaction::ofLastWeek(); // query transactions created during the last week
Transaction::monthToDate(); // query transactions created during the start of the current month till now
Transaction::ofLastYear(startFrom: '2020-01-01') // query transactions created during the last year, starting from 2020
// ... and much more scopes are available (see below)

// For sure, you can chain any Builder function you want here.
// Such as these aggregations, for instance:
Transaction::ofToday()->sum('amount');
Transaction::ofLastWeek()->avg('amount');
```

## ToC

- [`Installation`](#installation)
- [`Configuration`](#configuration)
- [`Scopes`](#scopes)

## Installation

Expand Down Expand Up @@ -57,7 +86,7 @@ return [
* 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)
* Transaction::ofLast7Days(customRange: 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),
Expand All @@ -80,14 +109,14 @@ directly 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);
Transaction::ofLast7Days(customRange: DateRange::INCLUSIVE);
```

This will do an inclusive query (today-6 days), even though the global default range here was set to exclusive.

### Fluent created_at column configuration

If you only want to change the ```created_at``` field in one of your models and not globally just do:
If you only want to change the ```created_at``` field in one of your models and not globally just do:

```php
use LaracraftTech\LaravelDateScopes\DateScopes;
Expand All @@ -100,10 +129,19 @@ class Transaction extends Model

const CREATED_AT = 'custom_created_at';
}
// also make sure to omit the defalt $table->timestamps() function in your migration
// also make sure to omit the default $table->timestamps() function in your migration
// and use something like this instead: $table->timestamp('custom_created_at')->nullable();
```

### Custom start date

If you want data not starting from now, but from another date, you can do this with:

```php
// query transactions created during 2019-2020
Transaction::ofLastYear(startFrom: '2020-01-01')
```

## Scopes

- [`seconds`](#seconds)
Expand All @@ -118,18 +156,6 @@ class Transaction extends Model
- [`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:

```php
use LaracraftTech\LaravelDateScopes\DateScopes;

class Transaction extends Model
{
use DateScopes;
}
```

### Seconds

Expand Down Expand Up @@ -280,15 +306,19 @@ Transaction::millenniumToDate(); // query transactions created during the start
composer test
```

## Changelog
## Upgrading

Please see [UPGRADING](UPGRADING.md) for details.

### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Contributing

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

## Security Vulnerabilities
## Security

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Expand Down
12 changes: 12 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Upgrading

## From v1 to v2

The `startFrom` parameter was added, so you may need to change the order of your passed arguments or use named arguments:

```php
// Instead of doing this:
Transaction::ofLast7Days(DateRange::INCLUSIVE);
// Do this:
Transaction::ofLast7Days(customRange: DateRange::INCLUSIVE);
```
2 changes: 1 addition & 1 deletion config/date-scopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* you could use the inclusive range here as a default.
* Note that you can also fluently specify it for quite every scope we offer
* directly when using the scope:
* Transaction::ofLast7Days(DateRange::INCLUSIVE); (this works for all but the singular "ofLast"-scopes)
* Transaction::ofLast7Days(customRange: 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),
Expand Down
Loading

0 comments on commit 4229cae

Please sign in to comment.