Skip to content

Instantly share code, notes, and snippets.

@Ellrion
Created September 13, 2016 10:02
Show Gist options
  • Save Ellrion/2edef2ebc29448a1f84a3d2af9beac2b to your computer and use it in GitHub Desktop.
Save Ellrion/2edef2ebc29448a1f84a3d2af9beac2b 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