执行Laravel getQueryLog显示行查询

时间:2014-08-13 16:38:58

标签: laravel

标题说我想知道是否有办法让getQueryLog函数显示查询行。

var_dump(DB::getQueryLog())

1 个答案:

答案 0 :(得分:7)

您无法使用getQueryLog()执行此操作,但您可以收听查询,生成回溯并最终提取行号:

// routes.php
Event::listen('illuminate.query', function($query,$binding,$time,$connections){

  $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

  foreach ($backtrace as $trace) {
    if(array_key_exists('file',$trace) && array_key_exists('line',$trace)){
      if( strpos($trace['file'],base_path().'/app') !== false ){
        var_dump(array(
          'query'    => $query
          ,'binding' => $binding
          ,'time'    => $time
          ,'connection' => $connections
          ,'file' => $trace['file']
          ,'line' => $trace['line']
        ));
        break;
      }
    }
  }
});