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中

posted @ 2019-10-20 11:29  onewaa  阅读(697)  评论(0编辑  收藏  举报