Skip to content

Commit 1288b7f

Browse files
support datetime value
1 parent d46f264 commit 1288b7f

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/ToRawSqlServiceProvider.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PyaeSoneAung\ToRawSql;
44

5+
use DateTimeInterface;
56
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Support\Str;
78
use Spatie\LaravelPackageTools\Package;
@@ -12,13 +13,17 @@ class ToRawSqlServiceProvider extends PackageServiceProvider
1213
public function boot(): void
1314
{
1415
Builder::macro('toRawSql', function (): string {
15-
/** @var \Illuminate\Database\Eloquent\Builder $this */
16-
$bindings = [];
17-
foreach ($this->getBindings() as $value) {
18-
if (is_string($value)) {
19-
$bindings[] = "'{$value}'";
20-
} else {
21-
$bindings[] = $value;
16+
/**
17+
* @var \Illuminate\Database\Eloquent\Builder $this
18+
*/
19+
$bindings = $this->getBindings();
20+
foreach ($bindings as $key => $value) {
21+
if ($value instanceof DateTimeInterface) {
22+
$bindings[$key] = "'{$value->format('Y-m-d H:i:s')}'";
23+
} elseif (is_bool($value)) {
24+
$bindings[$key] = (int) $value;
25+
} elseif (is_string($value)) {
26+
$bindings[$key] = "'{$value}'";
2227
}
2328
}
2429

tests/ToRawSqlTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
$query->where('name', 'Abigail')
99
->where('votes', '>', 50);
1010
})
11+
->where(
12+
'created_at',
13+
now()
14+
->setYear(2023)
15+
->setMonth(02)
16+
->setDay(10)
17+
->setHour(13)
18+
->setMinute(22)
19+
->setSecond(38)
20+
)
1121
->toRawSql()
12-
)->toBe("select * from `users` where `votes` > 100 or (`name` = 'Abigail' and `votes` > 50)");
22+
)->toBe("select * from `users` where `votes` > 100 or (`name` = 'Abigail' and `votes` > 50) and `created_at` = '2023-02-10 13:22:38'");
1323
});

0 commit comments

Comments
 (0)