Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel formatted query logging
Event::listen(QueryExecuted::class, function (QueryExecuted $query) {
// Format binding data for sql insertion
foreach ($query->bindings as $i => $binding) {
if ($binding instanceof \DateTime) {
$query->bindings[ $i ] = $binding->format('\'Y-m-d H:i:s\'');
} else {
if (is_string($binding)) {
$query->bindings[ $i ] = "'$binding'";
}
}
}
// Insert bindings into query
$boundSql = str_replace(['%', '?'], ['%%', '%s'], $query->sql);
$boundSql = vsprintf($boundSql, $query->bindings);
Log::debug(
"TIME - {$query->time}ms\n" .
" UNBOUND QUERY: {$query->sql};\n" .
" BOUND QUERY: $boundSql;"
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.