Laravel框架中打印sql

 在使用Laravel框架的时候,调试的时候,需要将查询的SQL输出校验,这是需要将SQL打印出来。

一、方法

DB::connection()->enableQueryLog();  // 开启查询日志

DB::table('my_table')->insert($data);        // 要查看的sql语句执行

$logs = DB::getQueryLog();                  // 获取查询日志

dd($logs);       // 即可查看执行的sql,传入的参数等等

二、举例

DB::connection()->enableQueryLog();
$res=DB::table('fre_questions')->where('fre_id',$fre_id)->first();
$logs = DB::getQueryLog();
dd($logs);

返回结果:

array:1 [
   0 => array:3 [
        "query" => "select * from `f_fre_questions` where `fre_id` = ? limit 1"
        "bindings" => array:1 [
              0 => "1"
         ]
        "time" => 11.0
   ]
]

猜你喜欢

转载自www.cnblogs.com/hld123/p/10177563.html