Skip to content

Instantly share code, notes, and snippets.

@rachids
Created July 20, 2022 21:05
Show Gist options
  • Save rachids/8013833dd4848449a843ec55a24ef377 to your computer and use it in GitHub Desktop.
Save rachids/8013833dd4848449a843ec55a24ef377 to your computer and use it in GitHub Desktop.
Laravel helper to print SQL Query with bindings
function printSqlQuery(
Builder|\Illuminate\Database\Query\Builder $builder,
bool $withBindings = true
): string {
$string = str_replace(['?', '%', '?'], ["'?'", "%%", '%s'], $builder->toSql());
$bindings = array_map(function ($value) {
if ($value === false) {
$value = 0;
}
return $value;
}, $builder->getBindings());
if ($withBindings && $string && $bindings) {
return vsprintf($string, $bindings);
}
return str_replace('%%', "%", $string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment