Last active
May 20, 2024 10:01
-
-
Save J2TeamNNL/350d6b2b7d5f9e6cb636e1c7428eca18 to your computer and use it in GitHub Desktop.
Listen query in Test
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 | |
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); | |
} | |
}); | |
} |
Author
J2TeamNNL
commented
May 20, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment