Skip to content

Instantly share code, notes, and snippets.

@zerolethanh
Created March 13, 2017 11:07
Show Gist options
  • Save zerolethanh/17a40f0e6fb6086fb55275b1d06cc6e2 to your computer and use it in GitHub Desktop.
Save zerolethanh/17a40f0e6fb6086fb55275b1d06cc6e2 to your computer and use it in GitHub Desktop.
<?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