Skip to content
Closed
87 changes: 75 additions & 12 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -731,6 +732,7 @@ public function truncate(): bool
* @param string $column
* @param string $key
* @return array
*
* @deprecated
*/
public function lists($column, $key = null)
Expand Down Expand Up @@ -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,
],
],
};
}

/**
Expand All @@ -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,
],
],
];
}

/**
Expand All @@ -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,
],
],
];
}

/**
Expand All @@ -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
],
],
];
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use Carbon\Carbon;

class QueryTest extends TestCase
{
protected static $started = false;
Expand Down
5 changes: 1 addition & 4 deletions tests/models/Birthday.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}