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 />';
    });
}
posted @ 2020-12-26 11:57  心之所依  阅读(700)  评论(0编辑  收藏  举报