Logs an SQL query

Submitted by eugene_santos - 10 years ago

logs a sql query with bindings replace by actual values

Event::listen('illuminate.query', function($sql, $bindings)
{
    foreach ($bindings as $i => $val) {
        $bindings[$i] = "'$val'";
    }
    
    $sql_with_bindings = array_reduce($bindings, function ($result, $item) {
        return substr_replace($result, $item, strpos($result, '?'), 1);
    }, $sql);

    Log::info($sql_with_bindings);
});