Last active
February 2, 2016 12:57
-
-
Save WesleyE/d32741e488e546461a6d to your computer and use it in GitHub Desktop.
Listen to all queries on a Capsule
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
$capsule->getDatabaseManager()->connection()->listen( | |
function ($query) { | |
// Fix bindings | |
$sql = $query->sql; | |
foreach ($query->bindings as $binding) { | |
//Find first occurence and replace | |
$sql = preg_replace('/\?+/', $binding, $sql, 1); | |
} | |
// Slow query detection | |
$time = $query->time.'ms'; | |
//Uncomment this one | |
echo '<script>console.log("DBQuery:", "'.$time.'", "'.preg_replace("/\r|\n/", " ", $sql).'")</script>'; | |
// We must trigger an flush in order to show these dumps | |
ob_flush(); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment