Last active
September 19, 2019 08:45
-
-
Save brkfun/3e5e0c7fbe12724a39e4a3e98affc76a to your computer and use it in GitHub Desktop.
Laravel get real query helper function
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
# 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