Skip to content

Instantly share code, notes, and snippets.

@dead23angel
Forked from Ellrion/helpers.php
Created September 14, 2016 02:07
Show Gist options
  • Save dead23angel/e284639e85f9a63626d3dd05789dd06d to your computer and use it in GitHub Desktop.
Save dead23angel/e284639e85f9a63626d3dd05789dd06d 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