Skip to content

Commit

Permalink
Merge pull request #2251 from yexk/master
Browse files Browse the repository at this point in the history
Add Model query whereDate support
  • Loading branch information
Smolevich committed May 12, 2021
2 parents 4056a73 + 016bb57 commit e16794d
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
------------
Expand Down Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions tests/models/Birthday.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

/**
* Class Birthday.
* @property string $name
* @property string $birthday
* @property string $day
* @property string $month
* @property string $year
* @property string $time
*/
class Birthday extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'birthday';
protected $fillable = ['name', 'birthday', 'day', 'month', 'year', 'time'];
}

0 comments on commit e16794d

Please sign in to comment.