Skip to content

Instantly share code, notes, and snippets.

@J2TeamNNL
Last active May 20, 2024 10:01
Show Gist options
  • Save J2TeamNNL/350d6b2b7d5f9e6cb636e1c7428eca18 to your computer and use it in GitHub Desktop.
Save J2TeamNNL/350d6b2b7d5f9e6cb636e1c7428eca18 to your computer and use it in GitHub Desktop.
Listen query in Test
<?php
function listenQuery($checkTrace = false): void
{
DB::listen(function ($query) use ($checkTrace) {
$sql = $query->sql;
foreach ($query->bindings as $binding) {
$value = is_numeric($binding) ? $binding : "'" . $binding . "'";
$sql = preg_replace('/\?/', $value, $sql, 1);
}
dump($sql); // Dump the SQL query with bindings
if($checkTrace){
// Lấy stack trace
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50);
$relevantTrace = [];
// Lọc stack trace để chỉ lấy những dòng có liên quan
foreach ($trace as $traceLine) {
if (isset($traceLine['file']) && strpos($traceLine['file'], 'vendor') === false) {
$relevantTrace[] = [
'file' => $traceLine['file'],
'line' => $traceLine['line'],
'function' => $traceLine['function'],
];
}
}
// Dump the relevant stack tracemp the SQL query with bindings
dump($relevantTrace);
}
});
}
@J2TeamNNL
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment