Skip to content

Instantly share code, notes, and snippets.

@allaniftrue
Created February 3, 2021 21:04
Show Gist options
  • Save allaniftrue/0bfb4bb8378e5edf5429fc309285cc49 to your computer and use it in GitHub Desktop.
Save allaniftrue/0bfb4bb8378e5edf5429fc309285cc49 to your computer and use it in GitHub Desktop.
Laravel Helpers
<?php
if (! function_exists('getSql')) {
/**
* @param $model
* @return string
*/
function getSql($model): string
{
$replace = function ($sql, $bindings)
{
$needle = '?';
foreach ($bindings as $replace){
$pos = strpos($sql, $needle);
if ($pos !== false) {
if (gettype($replace) === "string") {
$replace = ' "'.addslashes($replace).'" ';
}
$sql = substr_replace($sql, $replace, $pos, strlen($needle));
}
}
return $sql;
};
$sql = $replace($model->toSql(), $model->getBindings());
return $sql . "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment