Created
March 13, 2017 11:07
-
-
Save zerolethanh/17a40f0e6fb6086fb55275b1d06cc6e2 to your computer and use it in GitHub Desktop.
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 | |
if (!function_exists('dbStartLog')) { | |
function dbStartLog() | |
{ | |
app('db')->enableQueryLog(); | |
} | |
} | |
if (!function_exists('dbEndLog')) { | |
function dbEndLog(Closure $closure = null) | |
{ | |
$sqls = app('db')->getQueryLog(); | |
$sqls_string = ""; | |
foreach ($sqls as $sql) { | |
$bindings = $sql['bindings']; | |
// $time = $sql->time; | |
$sql = $sql['query']; | |
$sql = dbBindings($sql, $bindings); | |
$sqls_string .= $sql . '; ' . PHP_EOL; | |
} | |
if (isset($sqls_string)) { | |
app('log')->info($sqls_string); | |
if (!is_null($closure)) { | |
$closure($sqls_string); | |
} | |
return $sqls_string; | |
} | |
app('db')->disableQueryLog(); | |
} | |
function dbBindings($sql, array $bindings) | |
{ | |
if (empty($bindings)) { | |
return $sql; | |
} | |
$placeholder = preg_quote('?', '/'); | |
foreach ($bindings as $binding) { | |
$binding = is_numeric($binding) ? $binding : "'{$binding}'"; | |
$sql = preg_replace('/' . $placeholder . '/', $binding, $sql, 1); | |
} | |
return $sql; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment