diff --git a/README.md b/README.md index 808e266ec..360f30592 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,10 @@ This package adds functionalities to the Eloquent model and Query builder for Mo - [Configuration](#configuration) - [Eloquent](#eloquent) - [Extending the base model](#extending-the-base-model) + - [Extending the Authenticable base model](#extending-the-authenticable-base-model) - [Soft Deletes](#soft-deletes) - - [Dates](#dates) - [Guarding attributes](#guarding-attributes) + - [Dates](#dates) - [Basic Usage](#basic-usage) - [MongoDB-specific operators](#mongodb-specific-operators) - [MongoDB-specific Geo operations](#mongodb-specific-geo-operations) @@ -44,9 +45,10 @@ This package adds functionalities to the Eloquent model and Query builder for Mo - [Authentication](#authentication) - [Queues](#queues) - [Laravel specific](#laravel-specific) - - [Lumen specific](#Lumen-specific) + - [Lumen specific](#lumen-specific) - [Upgrading](#upgrading) - [Upgrading from version 2 to 3](#upgrading-from-version-2-to-3) + - [Security contact information](#security-contact-information) Installation ------------ @@ -356,6 +358,14 @@ $posts = Post::whereBetween('votes', [1, 100])->get(); $users = User::whereNull('age')->get(); ``` +**whereDate** + +```php +$users = User::whereDate('birthday', '2021-5-12')->get(); +``` +The usage is the same as `whereMonth` / `whereDay` / `whereYear` / `whereTime` + + **Advanced wheres** ```php diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 6eb910587..de3265cb6 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -1135,6 +1135,76 @@ protected function compileWhereBetween(array $where) ]; } + /** + * @param array $where + * @return array + */ + protected function compileWhereDate(array $where) + { + extract($where); + + $where['operator'] = $operator; + $where['value'] = $value; + + return $this->compileWhereBasic($where); + } + + /** + * @param array $where + * @return array + */ + protected function compileWhereMonth(array $where) + { + extract($where); + + $where['operator'] = $operator; + $where['value'] = $value; + + return $this->compileWhereBasic($where); + } + + /** + * @param array $where + * @return array + */ + protected function compileWhereDay(array $where) + { + extract($where); + + $where['operator'] = $operator; + $where['value'] = $value; + + return $this->compileWhereBasic($where); + } + + /** + * @param array $where + * @return array + */ + protected function compileWhereYear(array $where) + { + extract($where); + + $where['operator'] = $operator; + $where['value'] = $value; + + return $this->compileWhereBasic($where); + } + + /** + * @param array $where + * @return array + */ + protected function compileWhereTime(array $where) + { + extract($where); + + $where['operator'] = $operator; + $where['value'] = $value; + + return $this->compileWhereBasic($where); + } + /** * @param array $where * @return mixed diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 72a030ff6..cc22df587 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -18,12 +18,19 @@ public function setUp(): void User::create(['name' => 'Tommy Toe', 'age' => 33, 'title' => 'user']); User::create(['name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin']); User::create(['name' => 'Error', 'age' => null, 'title' => null]); + Birthday::create(['name' => 'Mark Moe', 'birthday' => '2020-04-10', 'day' => '10', 'month' => '04', 'year' => '2020', 'time' => '10:53:11']); + Birthday::create(['name' => 'Jane Doe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', 'year' => '2021', 'time' => '10:53:12']); + Birthday::create(['name' => 'Harry Hoe', 'birthday' => '2021-05-11', 'day' => '11', 'month' => '05', 'year' => '2021', 'time' => '10:53:13']); + Birthday::create(['name' => 'Robert Doe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', 'year' => '2021', 'time' => '10:53:14']); + Birthday::create(['name' => 'Mark Moe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', 'year' => '2021', 'time' => '10:53:15']); + Birthday::create(['name' => 'Mark Moe', 'birthday' => '2022-05-12', 'day' => '12', 'month' => '05', 'year' => '2022', 'time' => '10:53:16']); } public function tearDown(): void { User::truncate(); Scoped::truncate(); + Birthday::truncate(); parent::tearDown(); } @@ -163,6 +170,54 @@ public function testWhereNotNull(): void $this->assertCount(8, $users); } + public function testWhereDate(): void + { + $birthdayCount = Birthday::whereDate('birthday', '2021-05-12')->get(); + $this->assertCount(3, $birthdayCount); + + $birthdayCount = Birthday::whereDate('birthday', '2021-05-11')->get(); + $this->assertCount(1, $birthdayCount); + } + + public function testWhereDay(): void + { + $day = Birthday::whereDay('day', '12')->get(); + $this->assertCount(4, $day); + + $day = Birthday::whereDay('day', '11')->get(); + $this->assertCount(1, $day); + } + + public function testWhereMonth(): void + { + $month = Birthday::whereMonth('month', '04')->get(); + $this->assertCount(1, $month); + + $month = Birthday::whereMonth('month', '05')->get(); + $this->assertCount(5, $month); + } + + public function testWhereYear(): void + { + $year = Birthday::whereYear('year', '2021')->get(); + $this->assertCount(4, $year); + + $year = Birthday::whereYear('year', '2022')->get(); + $this->assertCount(1, $year); + + $year = Birthday::whereYear('year', '<', '2021')->get(); + $this->assertCount(1, $year); + } + + public function testWhereTime(): void + { + $time = Birthday::whereTime('time', '10:53:11')->get(); + $this->assertCount(1, $time); + + $time = Birthday::whereTime('time', '>=', '10:53:14')->get(); + $this->assertCount(3, $time); + } + public function testOrder(): void { $user = User::whereNotNull('age')->orderBy('age', 'asc')->first(); diff --git a/tests/models/Birthday.php b/tests/models/Birthday.php new file mode 100644 index 000000000..c30ebb746 --- /dev/null +++ b/tests/models/Birthday.php @@ -0,0 +1,21 @@ +