diff --git a/src/Query/Builder.php b/src/Query/Builder.php index f83bce3e2..30e6297cb 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -7,6 +7,7 @@ use Illuminate\Database\Query\Builder as BaseBuilder; use Illuminate\Database\Query\Expression; use Illuminate\Support\Arr; +use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; use Illuminate\Support\Str; @@ -731,6 +732,7 @@ public function truncate(): bool * @param string $column * @param string $key * @return array + * * @deprecated */ public function lists($column, $key = null) @@ -1163,10 +1165,45 @@ protected function compileWhereDate(array $where) { extract($where); - $where['operator'] = $operator; - $where['value'] = $value; + $startOfDay = new UTCDateTime(Carbon::parse($value)->startOfDay()); + $endOfDay = new UTCDateTime(Carbon::parse($value)->endOfDay()); - return $this->compileWhereBasic($where); + $operator = $this->conversion[$operator]; + + return match($operator) { + '=' => [ + $column => [ + '$gte' => $startOfDay, + '$lte' => $endOfDay, + ], + ], + '$ne' => [ + $column => [ + '$gt' => $endOfDay, + '$lt' => $startOfDay, + ], + ], + '$lt' => [ + $column => [ + '$lt' => $startOfDay, + ], + ], + '$gt' => [ + $column => [ + '$gt' => $endOfDay, + ], + ], + '$lte' => [ + $column => [ + '$lte' => $endOfDay, + ], + ], + '$gte' => [ + $column => [ + '$gte' => $startOfDay, + ], + ], + }; } /** @@ -1177,10 +1214,19 @@ protected function compileWhereMonth(array $where) { extract($where); - $where['operator'] = $operator; - $where['value'] = $value; + $operator = $operator === '=' ? '$eq' : $this->conversion[$operator]; + $value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value; - return $this->compileWhereBasic($where); + return [ + '$expr' => [ + $operator => [ + [ + '$month' => '$'.$column + ], + $value, + ], + ], + ]; } /** @@ -1191,10 +1237,19 @@ protected function compileWhereDay(array $where) { extract($where); - $where['operator'] = $operator; - $where['value'] = $value; + $operator = $operator === '=' ? '$eq' : $this->conversion[$operator]; + $value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value; - return $this->compileWhereBasic($where); + return [ + '$expr' => [ + $operator => [ + [ + '$dayOfMonth' => '$'.$column + ], + $value, + ], + ], + ]; } /** @@ -1205,10 +1260,18 @@ protected function compileWhereYear(array $where) { extract($where); - $where['operator'] = $operator; - $where['value'] = $value; + $operator = $operator === '=' ? '$eq' : $this->conversion[$operator]; - return $this->compileWhereBasic($where); + return [ + '$expr' => [ + $operator => [ + [ + '$year' => '$'.$column + ], + $value + ], + ], + ]; } /** diff --git a/tests/QueryTest.php b/tests/QueryTest.php index b2716e178..e1ed7fc12 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use Carbon\Carbon; + class QueryTest extends TestCase { protected static $started = false; diff --git a/tests/models/Birthday.php b/tests/models/Birthday.php index 3e725e495..ddb776d4c 100644 --- a/tests/models/Birthday.php +++ b/tests/models/Birthday.php @@ -9,14 +9,11 @@ * * @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']; + protected $fillable = ['name', 'birthday', 'time']; }