Skip to content

Commit

Permalink
Merge pull request #3 from omaralalwi/add-days-scopes
Browse files Browse the repository at this point in the history
add days scopes
  • Loading branch information
omaralalwi authored Aug 27, 2024
2 parents fa75154 + 428d78e commit 5d8da29
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,51 @@ You can apply various scopes in your model queries. Below are the descriptions,
$currentWeekOrders = Order::currentWeek()->get();
```

- **`last7Days`** : Filters records created in the last 7 days.

```php
$last7DaysOrders = Order::last7Days()->get();
```

- **`last10Days`** : Filters records created in the last 10 days.

```php
$last10DaysOrders = Order::last10Days()->get();
```

- **`last14Days`** : Filters records created in the last 14 days.

```php
$last14DaysOrders = Order::last14Days()->get();
```

- **`last15Days`** : Filters records created in the last 15 days.

```php
$last15DaysOrders = Order::last15Days()->get();
```

- **`last21Days`** : Filters records created in the last 21 days.

```php
$last21DaysOrders = Order::last21Days()->get();
```

- **`last30Days`** : Filters records created in the last 30 days.

```php
$last30DaysOrders = Order::last30Days()->get();
```

- **`lastDays($days)`** : Filters records created in the last number of days specified.

```php
// Filters records created in the last 5 days
$last5DaysOrders = Order::lastDays(null,5)->get(); // null mean take default field 'created_at' , or you can pass it 'created_at'
// Filters records created in the last 12 days
$last10DaysOrders = Order::lastDays(null,12)->get();
```

- **`oneMonthAgo`** : Filters records created in the last 30 days.

```php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"homepage": "https://github.com/omaralalwi/laravel-time-craft",
"license": "MIT",
"type": "library",
"version": "1.0.1",
"version": "1.0.2",
"authors": [
{
"name": "omar alalwi",
Expand Down
85 changes: 85 additions & 0 deletions src/Traits/HasDateTimeScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,91 @@ public function scopeCurrentWeek($query, $dateField = null): Builder
]);
}

/**
* Scope a query to include records created in the last 7 days.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|null $dateField
* @return \Illuminate\Database\Eloquent\Builder The query builder instance.
*/
public function scopeLast7Days($query, $dateField = null): Builder
{
return $query->whereDate($this->getDateField($dateField), '>=', now()->subDays(7));
}

/**
* Scope a query to include records created in the last 10 days.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|null $dateField
* @return \Illuminate\Database\Eloquent\Builder The query builder instance.
*/
public function scopeLast10Days($query, $dateField = null): Builder
{
return $query->whereDate($this->getDateField($dateField), '>=', now()->subDays(10));
}

/**
* Scope a query to include records created in the last 14 days.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|null $dateField
* @return \Illuminate\Database\Eloquent\Builder The query builder instance.
*/
public function scopeLast14Days($query, $dateField = null): Builder
{
return $query->whereDate($this->getDateField($dateField), '>=', now()->subDays(14));
}

/**
* Scope a query to include records created in the last 15 days.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|null $dateField
* @return \Illuminate\Database\Eloquent\Builder The query builder instance.
*/
public function scopeLast15Days($query, $dateField = null): Builder
{
return $query->whereDate($this->getDateField($dateField), '>=', now()->subDays(15));
}

/**
* Scope a query to include records created in the last 21 days.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|null $dateField
* @return \Illuminate\Database\Eloquent\Builder The query builder instance.
*/
public function scopeLast21Days($query, $dateField = null): Builder
{
return $query->whereDate($this->getDateField($dateField), '>=', now()->subDays(21));
}

/**
* Scope a query to include records created in the last 30 days.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|null $dateField
* @return \Illuminate\Database\Eloquent\Builder The query builder instance.
*/
public function scopeLast30Days($query, $dateField = null): Builder
{
return $query->whereDate($this->getDateField($dateField), '>=', now()->subDays(30));
}

/**
* Scope a query to include records created in the last number of days specified.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|null $dateField
* @param int $days The number of days to look back.
* @return \Illuminate\Database\Eloquent\Builder The query builder instance.
*/
public function scopeLastDays($query, $dateField = null, $days = 7): Builder
{
return $query->whereDate($this->getDateField($dateField), '>=', now()->subDays($days));
}

/**
* Scope a query to include records created in the last 30 days.
*
Expand Down

0 comments on commit 5d8da29

Please sign in to comment.