Laravel技巧集锦(16):使用DB::listen查找慢SQL
1、AppServiceProvider.php中
\DB::listen(function ($query){ $sql = $query->sql; $bindings = $query->bindings; $time = $query->time; if($time>10){ //when time > 10 print \Log::debug(var_export(compact(['sql','bindings','time']),true)); } });
2、注意
var_export(compact(['sql','bindings','time']))默认前台页面打印
var_export(compact(['sql','bindings','time']),true)将会存储在storage\logs\laravel.log中