laravel5.6如何打印SQL

方法一:

$sql = DB::table('my_table')->select()->tosql();

此方法支持 select 语句

方法二:

DB::connection()->enableQueryLog();
DB::table('my_table')->insert($data);
$logs = DB::getQueryLog();
dd($logs);

此方法支持 select 语句

方法三:

// 在需要打印SQL的语句前添加监听事件。
DB::listen(function($query) {
    $bindings = $query->bindings;
    $sql = $query->sql;
    foreach ($bindings as $replace){
        $value = is_numeric($replace) ? $replace : "'".$replace."'";
        $sql = preg_replace('/\?/', $value, $sql, 1);
    }
    dd($sql);
});
// 要打印SQL的语句
$res = DB::table('my_table')->insert($data);

此方法支持 insert, update, delete, select 等。

转载文章 https://learnku.com/articles/8654/how-does-laravel56-print-sql-summary-of-insertupdateselect-printing-method

posted @ 2019-09-01 19:05  学习everyday  阅读(2221)  评论(0编辑  收藏  举报