Skip to content

Instantly share code, notes, and snippets.

@ulcuber
Last active May 17, 2019 08:47
Convert laravel Builder to string with bindings
<?php
if (!function_exists('get_query_with_bindings')) {
function get_query_with_bindings($query): string
{
$sql = $query->toSql();
$replace = '?';
$replaceLength = strlen($replace);
foreach ($query->getBindings() as $binding) {
if (!is_int($binding)) {
$binding = addslashes($binding);
$binding = "'{$binding}'";
}
$pos = strpos($sql, $replace);
if ($pos !== false) {
$sql = substr_replace($sql, $binding, $pos, $replaceLength);
}
}
return $sql;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment