From 428d78ed66b5e4de2a10efe1ac91af27eeca952d Mon Sep 17 00:00:00 2001 From: omaralalwi Date: Tue, 27 Aug 2024 19:30:40 +0300 Subject: [PATCH] add days scopes --- README.md | 45 +++++++++++++++++ composer.json | 2 +- src/Traits/HasDateTimeScopes.php | 85 ++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8fb5fc8..035fb02 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json index 4736f9a..848ae93 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Traits/HasDateTimeScopes.php b/src/Traits/HasDateTimeScopes.php index e288f8c..7871e96 100644 --- a/src/Traits/HasDateTimeScopes.php +++ b/src/Traits/HasDateTimeScopes.php @@ -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. *