-
-
Save SergeyMiracle/c37182b9749611d8dc6bc492e357e4b9 to your computer and use it in GitHub Desktop.
Showing all database queries in Laravel 5.2+
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('dbd')) { | |
/** | |
* Showing all database queries. | |
* Отображение всех запросов в базу. | |
* | |
* @param null|\Illuminate\Console\Command|\Psr\Log\LoggerInterface $channel | |
*/ | |
function dbd($channel = null) | |
{ | |
static $initialized; | |
if ($initialized) { | |
return; | |
} | |
app('db')->listen(function ($sql) use ($channel) { | |
foreach ($sql->bindings as $i => $binding) { | |
$sql->bindings[$i] = is_string($binding) ? "'$binding'" : (string) $binding; | |
} | |
$query = str_replace(['%', '?'], ['%%', '%s'], $sql->sql); | |
$query = vsprintf($query, $sql->bindings); | |
if (null === $channel) { | |
dump($query); | |
} elseif ($channel instanceof \Illuminate\Console\Command) { | |
$channel->info($query); | |
} elseif ($channel instanceof \Psr\Log\LoggerInterface) { | |
$channel->info($query); | |
} | |
}); | |
$initialized = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment