Created
September 13, 2016 10:02
-
-
Save Ellrion/2edef2ebc29448a1f84a3d2af9beac2b to your computer and use it in GitHub Desktop.
Showing all database queries in Laravel 5.2+
This file contains 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