使用适当的绑定获取Laravel雄辩的查询

使用适当的绑定获取Laravel雄辩的查询,laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,当我dd(\DB::getQueryLog())时,我得到如下结果: array (size=3) 'query' => string 'select * from `clicks` where `clicks`.`user_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and `created_at` between ? and ?' (length=114) 'bindings' => array (size

当我
dd(\DB::getQueryLog())
时,我得到如下结果:

array (size=3)
      'query' => string 'select * from `clicks` where `clicks`.`user_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and `created_at` between ? and ?' (length=114)
      'bindings' => 
        array (size=12)
          0 => int 70
          1 => int 69
          2 => int 68
          3 => int 67
          4 => int 66
          5 => int 65
          6 => int 64
          7 => int 63
          8 => int 60
          9 => int 58
          10 => 
            object(Carbon\Carbon)[681]
              public 'date' => string '2014-12-27 20:06:39.000000' (length=26)
              public 'timezone_type' => int 3
              public 'timezone' => string 'UTC' (length=3)
          11 => 
            object(Carbon\Carbon)[684]
              public 'date' => string '2015-01-26 20:06:39.000000' (length=26)
              public 'timezone_type' => int 3
              public 'timezone' => string 'UTC' (length=3)
      'time' => float 932.67

如何在所有绑定都就绪的情况下获取查询,这样我就可以将其复制到MySQL Workbench并对其进行修补,而无需每次手动添加这些绑定?

实际的SQL字符串在PHP端永远不可用。您可以尝试手动解决问题,并使用绑定替换

echo vsprintf(str_replace('?', '%s', $queryString), $bindings);
我在控制器中创建了一个函数来执行此操作:

我的职能:

公共函数showExecutedQueries($queryLog){
foreach($queryLog作为$query){
$aQuery[]=vsprintf(str_替换(“?”、“%s”、$query['query'])、$query['bindings']);
}
echo'';print_r($aQuery);die();
}

它将在数组中按顺序显示所有执行的查询

嗨,欢迎来到StackOverflow!请注意,这个问题已经有一个公认的答案。如果需要,请给出您的答案,以确保它比本问题中已有的其他答案有所改进。
public function create(Request $request){ //you are in some function of your controller
    DB::enableQueryLog(); //enable query log
      SomeModel::create($request->all()); //your query running
    $queryLog = DB::getQueryLog(); //get query log
    $this->showExecutedQueries($queryLog); //calling my function
}
public function showExecutedQueries($queryLog){
        foreach ($queryLog as $query){
            $aQuery[] = vsprintf(str_replace('?', '%s',$query['query']),$query['bindings']);
        }
        echo '<pre>'; print_r($aQuery); die();
    }