Logs an SQL query

Submitted by eugene_santos - 3 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);
});

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks