Laravel 打印SQL

打印SQL默认是关闭的,需要在/vendor/laravel/framework/src/illuminate/database/Connection.php中打开。

/**
     * Indicates whether queries are being logged.
     *
     * @var bool
     */
//    protected $loggingQueries = false; //默认关闭
    protected $loggingQueries = true;

然后就可以在代码中使用:

public function index()
{ $result = DB::select('select * from activity'); $log = DB::getQueryLog(); dd($log); }

如果不想开启但需要临时查看,可以这样操作:

public function index()
{
        DB::connection()->enableQueryLog(); // 需要加多这行
        
        $result = DB::select('select * from activity');

        $log = DB::getQueryLog();
        dd($log);
}

 

posted @ 2021-02-26 16:50  一粒小米-博客  阅读(1132)  评论(0编辑  收藏  举报