Skip to content

Instantly share code, notes, and snippets.

@BinaryKitten
Last active January 22, 2023 21:52
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinaryKitten/2873e11daf3c0130b5a19f6b94315033 to your computer and use it in GitHub Desktop.
Save BinaryKitten/2873e11daf3c0130b5a19f6b94315033 to your computer and use it in GitHub Desktop.
toRawSql.php
<?php
declare(strict_types=1);
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
QueryBuilder::macro(
'toRawSql',
function () {
return array_reduce(
$this->getBindings(),
static function ($sql, $binding) {
if ($binding instanceof DateTime) {
$binding = $binding->format('Y-m-d H:i:s');
}
return preg_replace(
'/\?/',
is_string($binding) ? "'" . $binding . "'" : $binding,
$sql,
1
);
},
$this->toSql()
);
}
);
EloquentBuilder::macro(
'toRawSql',
function () {
return $this->getQuery()->toRawSql();
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment