├── config └── config.php ├── src ├── FlexibleWhereBetween.php ├── Builder │ └── FlexibleWhereBetweenBuilder.php └── FlexibleWhereBetweenServiceProvider.php ├── LICENSE.md ├── composer.json └── README.md /config/config.php: -------------------------------------------------------------------------------- 1 | getConnection(); 20 | 21 | return new QueryBuilder($connection, $connection->getQueryGrammar(), $connection->getPostProcessor()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/Builder/FlexibleWhereBetweenBuilder.php: -------------------------------------------------------------------------------- 1 | where($column, '<=', $max, $boolean); 22 | } 23 | 24 | if (is_null($max)) { 25 | return $this->where($column, '>=', $min, $boolean); 26 | } 27 | 28 | $this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not'); 29 | 30 | $this->addBinding($this->cleanBindings($values), 'where'); 31 | 32 | return $this; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Jerred Hurst 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 all 13 | 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 THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /src/FlexibleWhereBetweenServiceProvider.php: -------------------------------------------------------------------------------- 1 | where($column, '<=', $max, $boolean); 27 | } 28 | 29 | if (is_null($max)) { 30 | return $this->where($column, '>=', $min, $boolean); 31 | } 32 | 33 | $this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not'); 34 | 35 | $this->addBinding($this->cleanBindings($values), 'where'); 36 | 37 | return $this; 38 | }); 39 | } 40 | 41 | /** 42 | * Register the application services. 43 | */ 44 | public function register() 45 | { 46 | // 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "primitive/flexible-where-between", 3 | "description": "", 4 | "keywords": [ 5 | "primitive", 6 | "laravel", 7 | "whereBetween", 8 | "eloquent", 9 | "querybuilder", 10 | "flexible-where-between" 11 | ], 12 | "homepage": "https://github.com/primitivesocial/flexible-where-between", 13 | "license": "MIT", 14 | "type": "library", 15 | "authors": [ 16 | { 17 | "name": "Jerred Hurst", 18 | "email": "jerred@leadwithprimitive.com", 19 | "homepage": "https://leadwihtprimitive.com", 20 | "role": "Developer" 21 | } 22 | ], 23 | "require": { 24 | "php": "^7.1", 25 | "illuminate/support": "~7.0|~8.0" 26 | }, 27 | "require-dev": { 28 | "orchestra/testbench": "^3.8|^4.0|^5.0|^6.0", 29 | "phpunit/phpunit": "^8.0|^9.0" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "Primitive\\FlexibleWhereBetween\\": "src" 34 | } 35 | }, 36 | "autoload-dev": { 37 | "psr-4": { 38 | "Primitive\\FlexibleWhereBetween\\Tests\\": "tests" 39 | } 40 | }, 41 | "config": { 42 | "sort-packages": true 43 | }, 44 | "extra": { 45 | "laravel": { 46 | "providers": [ 47 | "Primitive\\FlexibleWhereBetween\\FlexibleWhereBetweenServiceProvider" 48 | ], 49 | "aliases": { 50 | "FlexibleWhereBetween": "Primitive\\FlexibleWhereBetween\\FlexibleWhereBetweenFacade" 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/primitive/flexible-where-between.svg?style=flat-square)](https://packagist.org/packages/primitive/flexible-where-between) 3 | [![Total Downloads](https://img.shields.io/packagist/dt/primitives/flexible-where-between.svg?style=flat-square)](https://packagist.org/packages/primitive/flexible-where-between) 4 | 5 | ![image](https://user-images.githubusercontent.com/13042804/97702236-a4ea0880-1a7c-11eb-940a-ee99796f6044.png) 6 | 7 | 8 | We created this package to avoid duplication of code accross projects when using [Laravel's](https://laravel.com) `whereBetween` method. 9 | 10 | For example, if you are looking for "something" between two dates, your method without this package would need to look like: 11 | 12 | ``` php 13 | $logs = Log::query(); 14 | 15 | if ((empty($end_date) && (empty($start_date)) 16 | { 17 | $logs = $logs->get(); 18 | } 19 | 20 | else if (empty($end_date)) 21 | { 22 | $logs = $logs->where('created_at','>=', $start_date); 23 | } 24 | else if (empty($start_date)) 25 | { 26 | $logs = $logs->where('created_at','<=', $end_date); 27 | } 28 | else 29 | { 30 | $logs = $logs->whereBetween('created_at', [$start_date, $end_date]) 31 | } 32 | 33 | ``` 34 | 35 | This package takes care of that logic for you. Your new method would look like: 36 | 37 | 38 | ``` php 39 | Log::whereBetween('created_at', [$start_date, $end_date]) 40 | 41 | ``` 42 | So, regardless of whether `$start_date` or `$end_date` is `NULL` or has a value, it will "just work". 43 | 44 | 45 | ## Installation 46 | 47 | You can install the package via composer: 48 | 49 | ```bash 50 | composer require primitive/flexible-where-between 51 | ``` 52 | 53 | ## Usage 54 | 55 | After the package is installed, a `macro` is registered that overrides the default `whereBetween` functionality. So, it just works. :) 56 | 57 | ## Credits 58 | 59 | - [Primitive](https://github.com/primitivesocial) 60 | 61 | ## License 62 | 63 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 64 | --------------------------------------------------------------------------------