Laravel 5.3 - How to log all queries on a page?

后端 未结 7 1482
生来不讨喜
生来不讨喜 2020-12-24 12:48

My team and I are working on a rather big project. There\'s queries going on everywhere - in controllers, in view composers in views (lazy loading) and probably in some othe

相关标签:
7条回答
  • 2020-12-24 13:40

    Put this code right above the code where your query is executed

    \DB::listen(function($sql) {
      die(\Illuminate\Support\Str::replaceArray('?', $sql->bindings, $sql->sql));
    });
    

    Just modified for executable query:

    \DB::listen(function ($query) {
       // Enclose in single quotes for string params.
       $bindings = collect($query->bindings)->map(function ($param) {
          if(is_numeric($param)) {
            return $param;
          } else {
            return "'$param'";
          }
       });
    
       \Log::info(\Illuminate\Support\Str::replaceArray('?', $bindings->toArray(), $query->sql));
    });
    
    0 讨论(0)
提交回复
热议问题