Skip to content

Instantly share code, notes, and snippets.

@brkfun
Last active September 19, 2019 08:45
Show Gist options
  • Save brkfun/3e5e0c7fbe12724a39e4a3e98affc76a to your computer and use it in GitHub Desktop.
Save brkfun/3e5e0c7fbe12724a39e4a3e98affc76a to your computer and use it in GitHub Desktop.
Laravel get real query helper function
# Indeed you have to send builder to get bindings included to your query to make you to see real query goes to your database.
# Not much powerful it is just for seeing a singular query which is not working well that if you think
# write this function down to your helper(s).php
if(!function_exists('getRealQuery')){
function getRealQuery(\Illuminate\Database\Eloquent\Builder $query, $dumpIt = false)
{
$params = [];
foreach ($query->getBindings() as $binding) {
$params[] = '\''.$binding.'\'';
}
$queryNeedleArray = explode('?', $query->toSql());
$result = '';
foreach ($queryNeedleArray as $key => $queryBlob) {
$result .= $queryBlob . ($params[$key] ?? null);
}
if ($dumpIt) {
dump($result);
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment