Skip to content

Instantly share code, notes, and snippets.

@WesleyE
Last active February 2, 2016 12:57
Show Gist options
  • Save WesleyE/d32741e488e546461a6d to your computer and use it in GitHub Desktop.
Save WesleyE/d32741e488e546461a6d to your computer and use it in GitHub Desktop.
Listen to all queries on a Capsule
$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