Skip to content

Instantly share code, notes, and snippets.

@OwenMelbz
Last active September 14, 2017 10:30
Show Gist options
  • Save OwenMelbz/6bd5d9b6dcb27d48a9ea4ed028bc45dd to your computer and use it in GitHub Desktop.
Save OwenMelbz/6bd5d9b6dcb27d48a9ea4ed028bc45dd to your computer and use it in GitHub Desktop.
Dump Full Query Macro
<?php
/*
* Why
*
* If you're trying to debug queries sometimes its a pain to figure out if the actual
* query is correct as things like ->toSql dont provide the real values
* then ->getBindings returns objects like carbon etc which makes it hard.
*
* This tries to help by giving you a more realistic query output
*/
// Make sure this is called somewhere, probs a service provider
\Illuminate\Database\Query\Builder::macro('dumpQuery', function () {
$statement = str_replace(' ?', ' %s', $this->toSql());
$bindings = $this->getBindings();
foreach ($bindings as &$binding) {
if (is_string($binding) && !mb_check_encoding($binding, 'UTF-8')) {
$binding = '[BINARY DATA]';
}
$binding = htmlentities($binding, ENT_QUOTES, 'UTF-8', false);
}
dd(sprintf($statement, ...$bindings));
});
// Usage
User::where('username', 'my-username')->dumpQuery();
// select * from users where username = 'my-username'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment