Last active
May 17, 2019 08:47
-
-
Save ulcuber/56dfb7abb3c89bc5e521cd522525b042 to your computer and use it in GitHub Desktop.
Convert laravel Builder to string with bindings
This file contains hidden or 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
<?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