Skip to content

Commit c2197bb

Browse files
committed
(+) fix whereDate, whereMonth, whereYear, whereTime to use $expr and respective query rather than using basic comparison
1 parent f4c448f commit c2197bb

File tree

1 file changed

+101
-16
lines changed

1 file changed

+101
-16
lines changed

src/Query/Builder.php

Lines changed: 101 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Query\Builder as BaseBuilder;
88
use Illuminate\Database\Query\Expression;
99
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Carbon;
1011
use Illuminate\Support\Collection;
1112
use Illuminate\Support\LazyCollection;
1213
use Illuminate\Support\Str;
@@ -1163,10 +1164,40 @@ protected function compileWhereDate(array $where)
11631164
{
11641165
extract($where);
11651166

1166-
$where['operator'] = $operator;
1167-
$where['value'] = $value;
1167+
$date = Carbon::parse($value);
11681168

1169-
return $this->compileWhereBasic($where);
1169+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
1170+
1171+
return [
1172+
'$expr' => [
1173+
'$and' => [
1174+
[
1175+
$operator => [
1176+
[
1177+
'$dayOfMonth' => '$'.$column
1178+
],
1179+
$date->day
1180+
],
1181+
],
1182+
[
1183+
$operator => [
1184+
[
1185+
'$month' => '$'.$column
1186+
],
1187+
$date->month
1188+
],
1189+
],
1190+
[
1191+
$operator => [
1192+
[
1193+
'$month' => '$'.$column
1194+
],
1195+
$date->year
1196+
],
1197+
],
1198+
],
1199+
],
1200+
];
11701201
}
11711202

11721203
/**
@@ -1177,10 +1208,18 @@ protected function compileWhereMonth(array $where)
11771208
{
11781209
extract($where);
11791210

1180-
$where['operator'] = $operator;
1181-
$where['value'] = $value;
1211+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
11821212

1183-
return $this->compileWhereBasic($where);
1213+
return [
1214+
'$expr' => [
1215+
$operator => [
1216+
[
1217+
'$month' => '$'.$column
1218+
],
1219+
$value,
1220+
],
1221+
],
1222+
];
11841223
}
11851224

11861225
/**
@@ -1191,10 +1230,18 @@ protected function compileWhereDay(array $where)
11911230
{
11921231
extract($where);
11931232

1194-
$where['operator'] = $operator;
1195-
$where['value'] = $value;
1233+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
11961234

1197-
return $this->compileWhereBasic($where);
1235+
return [
1236+
'$expr' => [
1237+
$operator => [
1238+
[
1239+
'$dayOfMonth' => '$'.$column
1240+
],
1241+
$value,
1242+
],
1243+
],
1244+
];
11981245
}
11991246

12001247
/**
@@ -1204,11 +1251,19 @@ protected function compileWhereDay(array $where)
12041251
protected function compileWhereYear(array $where)
12051252
{
12061253
extract($where);
1254+
1255+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
12071256

1208-
$where['operator'] = $operator;
1209-
$where['value'] = $value;
1210-
1211-
return $this->compileWhereBasic($where);
1257+
return [
1258+
'$expr' => [
1259+
$operator => [
1260+
[
1261+
'$year' => '$'.$column
1262+
],
1263+
$value
1264+
],
1265+
],
1266+
];
12121267
}
12131268

12141269
/**
@@ -1219,10 +1274,40 @@ protected function compileWhereTime(array $where)
12191274
{
12201275
extract($where);
12211276

1222-
$where['operator'] = $operator;
1223-
$where['value'] = $value;
1277+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
12241278

1225-
return $this->compileWhereBasic($where);
1279+
$time = Carbon::parse($value);
1280+
1281+
return [
1282+
'$expr' => [
1283+
'$and' => [
1284+
[
1285+
$operator => [
1286+
[
1287+
'$hour' => '$'.$column
1288+
],
1289+
$time->hour
1290+
],
1291+
],
1292+
[
1293+
$operator => [
1294+
[
1295+
'$minute' => '$'.$column
1296+
],
1297+
$time->minute
1298+
],
1299+
],
1300+
[
1301+
$operator => [
1302+
[
1303+
'$second' => '$'.$column
1304+
],
1305+
$time->second
1306+
],
1307+
],
1308+
],
1309+
],
1310+
];
12261311
}
12271312

12281313
/**

0 commit comments

Comments
 (0)