├── .github └── workflows │ └── build.yml ├── LICENSE.md ├── README.md ├── composer.json ├── phpunit.xml └── src └── DateRange.php /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | on: [ push, pull_request ] 3 | jobs: 4 | run: 5 | runs-on: ubuntu-latest 6 | strategy: 7 | matrix: 8 | php-versions: [ '8.0', '8.1' ] 9 | name: Testing on PHP ${{ matrix.php-versions }} 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v2 13 | - name: Setup PHP 14 | uses: shivammathur/setup-php@v2 15 | with: 16 | php-version: ${{ matrix.php-versions }} 17 | extensions: mbstring,bcmath 18 | tools: phpunit,composer 19 | - name: Install dependencies 20 | run: composer install --quiet --no-ansi --no-interaction --no-scripts --no-progress 21 | - name: Run tests 22 | run: composer test 23 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Hedi Chaibi (hedii) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://github.com/hedii/laravel-date-range/workflows/Tests/badge.svg)](https://github.com/hedii/laravel-date-range/actions) 2 | [![Total Downloads](https://poser.pugx.org/hedii/laravel-date-range/downloads)](//packagist.org/packages/hedii/laravel-date-range) 3 | [![License](https://poser.pugx.org/hedii/laravel-date-range/license)](//packagist.org/packages/hedii/laravel-date-range) 4 | [![Latest Stable Version](https://poser.pugx.org/hedii/laravel-date-range/v)](//packagist.org/packages/hedii/laravel-date-range) 5 | 6 | # Laravel Date Range 7 | 8 | A date range trait with local scope methods for Laravel Eloquent models 9 | 10 | ## Table of contents 11 | 12 | - [Table of contents](#table-of-contents) 13 | - [Installation](#installation) 14 | - [Usage](#usage) 15 | - [Updating your Eloquent Models](#updating-your-eloquent-models) 16 | - [Available methods](#available-methods) 17 | - [Example](#example) 18 | - [Testing](#testing) 19 | - [License](#license) 20 | 21 | ## Installation 22 | 23 | Install via [composer](https://getcomposer.org/doc/00-intro.md) 24 | ```sh 25 | composer require hedii/laravel-date-range 26 | ``` 27 | 28 | ## Usage 29 | 30 | ### Updating your Eloquent Models 31 | 32 | Simply tell your eloquent model that it has to use the DateRange trait: 33 | 34 | ```php 35 | get(); 79 | $lastYearEntries = MyModel::lastYear()->get(); 80 | $entriesFromTheLastTenDays = MyModel::lastDays(10)->get(); 81 | ``` 82 | 83 | ## Testing 84 | 85 | ``` 86 | composer test 87 | ``` 88 | 89 | ## License 90 | 91 | hedii/laravel-date-range is released under the MIT Licence. See the bundled [LICENSE](https://github.com/hedii/laravel-date-range/blob/master/LICENSE.md) file for details. 92 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hedii/laravel-date-range", 3 | "description": "A date range trait with local scope methods for Laravel Eloquent models", 4 | "type": "library", 5 | "license": "MIT", 6 | "keywords": ["hedii", "laravel-date-range", "laravel", "trait", "php"], 7 | "authors": [ 8 | { 9 | "name": "hedii", 10 | "email": "hedi.chaibs@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "php": "^8.0", 15 | "illuminate/database": "^9.0", 16 | "illuminate/support": "^9.0", 17 | "nesbot/carbon": "^2.53" 18 | }, 19 | "require-dev": { 20 | "orchestra/testbench": "^7.0" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Hedii\\LaravelDateRange\\": "src/" 25 | } 26 | }, 27 | "autoload-dev": { 28 | "psr-4": { 29 | "Hedii\\LaravelDateRange\\Tests\\": "tests/" 30 | } 31 | }, 32 | "config": { 33 | "sort-packages": true 34 | }, 35 | "scripts": { 36 | "test": "vendor/bin/phpunit" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | ./tests/ 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/DateRange.php: -------------------------------------------------------------------------------- 1 | whereBetween($fieldName, [ 13 | Carbon::now()->second(0), 14 | Carbon::now(), 15 | ]); 16 | } 17 | 18 | public function scopeLastMinute(Builder $query, string $fieldName = 'created_at'): Builder 19 | { 20 | return $query->whereBetween($fieldName, [ 21 | Carbon::now()->subMinute()->second(0), 22 | Carbon::now()->second(0), 23 | ]); 24 | } 25 | 26 | public function scopeCurrentHour(Builder $query, string $fieldName = 'created_at'): Builder 27 | { 28 | return $query->whereBetween($fieldName, [ 29 | Carbon::now()->minute(0)->second(0), 30 | Carbon::now(), 31 | ]); 32 | } 33 | 34 | public function scopeLastHour(Builder $query, string $fieldName = 'created_at'): Builder 35 | { 36 | return $query->whereBetween($fieldName, [ 37 | Carbon::now()->subHour()->minute(0)->second(0), 38 | Carbon::now()->minute(0)->second(0), 39 | ]); 40 | } 41 | 42 | public function scopeCurrentDay(Builder $query, string $fieldName = 'created_at'): Builder 43 | { 44 | return $query->whereBetween($fieldName, [ 45 | Carbon::now()->startOfDay(), 46 | Carbon::now(), 47 | ]); 48 | } 49 | 50 | public function scopeLastDay(Builder $query, string $fieldName = 'created_at'): Builder 51 | { 52 | return $query->whereBetween($fieldName, [ 53 | Carbon::now()->subDay()->startOfDay(), 54 | Carbon::now()->startOfDay(), 55 | ]); 56 | } 57 | 58 | public function scopeCurrentWeek(Builder $query, string $fieldName = 'created_at'): Builder 59 | { 60 | return $query->whereBetween($fieldName, [ 61 | Carbon::now()->startOfWeek(), 62 | Carbon::now(), 63 | ]); 64 | } 65 | 66 | public function scopeLastWeek(Builder $query, string $fieldName = 'created_at'): Builder 67 | { 68 | return $query->whereBetween($fieldName, [ 69 | Carbon::now()->subWeek()->startOfWeek(), 70 | Carbon::now()->subWeek()->endOfWeek(), 71 | ]); 72 | } 73 | 74 | public function scopeCurrentMonth(Builder $query, string $fieldName = 'created_at'): Builder 75 | { 76 | return $query->whereBetween($fieldName, [ 77 | Carbon::now()->startOfMonth(), 78 | Carbon::now(), 79 | ]); 80 | } 81 | 82 | public function scopeLastMonth(Builder $query, string $fieldName = 'created_at'): Builder 83 | { 84 | return $query->whereBetween($fieldName, [ 85 | Carbon::now()->subMonth()->startOfMonth(), 86 | Carbon::now()->subMonth()->endOfMonth(), 87 | ]); 88 | } 89 | 90 | public function scopeCurrentYear(Builder $query, string $fieldName = 'created_at'): Builder 91 | { 92 | return $query->whereBetween($fieldName, [ 93 | Carbon::now()->startOfYear(), 94 | Carbon::now(), 95 | ]); 96 | } 97 | 98 | public function scopeLastYear(Builder $query, string $fieldName = 'created_at'): Builder 99 | { 100 | return $query->whereBetween($fieldName, [ 101 | Carbon::now()->subYear()->startOfYear(), 102 | Carbon::now()->subYear()->endOfYear(), 103 | ]); 104 | } 105 | 106 | /** 107 | * Scope a query to only include the last x seconds entries. 108 | */ 109 | public function scopeLastSeconds(Builder $query, int $countSeconds, string $fieldName = 'created_at'): Builder 110 | { 111 | return $query->whereBetween($fieldName, [ 112 | Carbon::now()->subSeconds($countSeconds), 113 | Carbon::now(), 114 | ]); 115 | } 116 | 117 | /** 118 | * Scope a query to only include the last x minutes entries. 119 | */ 120 | public function scopeLastMinutes(Builder $query, int $countMinutes, string $fieldName = 'created_at'): Builder 121 | { 122 | return $query->whereBetween($fieldName, [ 123 | Carbon::now()->subMinutes($countMinutes)->second(0), 124 | Carbon::now(), 125 | ]); 126 | } 127 | 128 | /** 129 | * Scope a query to only include the last x hours entries. 130 | */ 131 | public function scopeLastHours(Builder $query, int $countHours, string $fieldName = 'created_at'): Builder 132 | { 133 | return $query->whereBetween($fieldName, [ 134 | Carbon::now()->subHours($countHours)->minute(0), 135 | Carbon::now(), 136 | ]); 137 | } 138 | 139 | /** 140 | * Scope a query to only include the last x days entries. 141 | */ 142 | public function scopeLastDays(Builder $query, int $countDays, string $fieldName = 'created_at'): Builder 143 | { 144 | return $query->whereBetween($fieldName, [ 145 | Carbon::now()->subDays($countDays)->startOfDay(), 146 | Carbon::now(), 147 | ]); 148 | } 149 | 150 | /** 151 | * Scope a query to only include the last x weeks entries. 152 | */ 153 | public function scopeLastWeeks(Builder $query, int $countWeeks, string $fieldName = 'created_at'): Builder 154 | { 155 | return $query->whereBetween($fieldName, [ 156 | Carbon::now()->subWeeks($countWeeks)->startOfWeek(), 157 | Carbon::now(), 158 | ]); 159 | } 160 | 161 | /** 162 | * Scope a query to only include the last x months entries. 163 | */ 164 | public function scopeLastMonths(Builder $query, int $countMonths, string $fieldName = 'created_at'): Builder 165 | { 166 | return $query->whereBetween($fieldName, [ 167 | Carbon::now()->subMonths($countMonths)->startOfMonth(), 168 | Carbon::now(), 169 | ]); 170 | } 171 | 172 | /** 173 | * Scope a query to only include the last x years entries. 174 | */ 175 | public function scopeLastYears(Builder $query, int $countYears, string $fieldName = 'created_at'): Builder 176 | { 177 | return $query->whereBetween($fieldName, [ 178 | Carbon::now()->subYears($countYears)->startOfYear(), 179 | Carbon::now(), 180 | ]); 181 | } 182 | } 183 | --------------------------------------------------------------------------------