Skip to content

Instantly share code, notes, and snippets.

@SergeyMiracle
Forked from Ellrion/helpers.php
Created September 13, 2016 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergeyMiracle/c37182b9749611d8dc6bc492e357e4b9 to your computer and use it in GitHub Desktop.
Save SergeyMiracle/c37182b9749611d8dc6bc492e357e4b9 to your computer and use it in GitHub Desktop.
Showing all database queries in Laravel 5.2+
<?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