laravel打印sql所执行的原生语句

时间:2022-02-19 04:30:13
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);
    var_dump($query);
});