方法一:

$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

相关文章:

  • 2021-09-05
  • 2021-12-15
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2023-03-31
猜你喜欢
  • 2021-10-19
  • 2022-01-19
  • 2021-06-12
  • 2021-12-25
  • 2022-01-01
  • 2021-11-12
相关资源
相似解决方案