Laravel5打印所有运行SQL
//function //laravel 官方打印sql public static function printAllSql(){ DB::listen(function($query) { $sql_arr = []; $bindings = $query->bindings; $sql = $query->sql; foreach ($bindings as $replace){ $value = is_numeric($replace) ? $replace : "'".$replace."'"; $sql = preg_replace('/\?/', $value, $sql, 1); } $sql_arr[] = $sql; print_r($sql_arr); }); }
//调用方式:直接在请求流的最上面调用方法即可
printAllSql();
第二种:
DB::connection()->enableQueryLog();#开启执行日志 $result = DB::table('advert')->whereJsonContains('tag',"1")->get(); dd(DB::getQueryLog());