Created
July 20, 2022 21:05
-
-
Save rachids/8013833dd4848449a843ec55a24ef377 to your computer and use it in GitHub Desktop.
Laravel helper to print SQL Query with bindings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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