数据库
首页 > 数据库> > Laravel 打印SQL日志

Laravel 打印SQL日志

作者:互联网

function getLastSql()
{
    DB::listen(function ($sql) {
        foreach ($sql->bindings as $i => $binding) {
            if ($binding instanceof \DateTime) {
                $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
            } else {
                if (is_string($binding)) {
                    $sql->bindings[$i] = "'$binding'";
                }
            }
        }
        $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);
        $query = vsprintf($query, $sql->bindings);
        dd($query);
        echo '<br />';
    });
}

标签:Laravel,function,array,SQL,binding,sql,query,日志,bindings
来源: https://www.cnblogs.com/sgm4231/p/14192019.html