laravel 开启sql调试

打开app\Providers\AppServiceProvider.PHP,在boot方法中添加如下内容

 

public function boot()
{
    //sql调试
    $sql_debug = config('database.sql_debug');
    if ($sql_debug) {
        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);
            print_r($query);
            echo '<br />';
        });
    }
}

 

在app/config/database.php 添加

'sql_debug' => 0, //为1输出所有执行的sql语句

 

 

 

或者在app/helper.php函数添加

 

 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);
        print_r($query);
        echo '<br />';
    });
}

在数据库操作语句之前调用即可打印

posted @ 2018-03-12 12:54  爱你爱自己  阅读(2792)  评论(0编辑  收藏  举报