Laravel 5雄辩:如何获取正在执行的原始sql? (具有绑定的数据) [英] Laravel 5 Eloquent: How to get raw sql that is being executed? (with binded data)

查看:230
本文介绍了Laravel 5雄辩:如何获取正在执行的原始sql? (具有绑定的数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何获取正在执行的原始sql查询,包括其中的绑定数据.香港专业教育学院得到了什么:

Im trying to figure out how to get the raw sql query being executed including the binded data in it. Here is what ive got:

\DB::connection()->enableQueryLog();
$query = \DB::getQueryLog();
$lastQuery = end($query);

结果是这样的:

array(3) {
  ["query"]=>
  string(57) "select * from `table_1` where `field_1` = ? limit 1"
  ["bindings"]=>
  array(1) {
    [0]=>
    string(34) "xyz"
  }
}

那么我如何像这样(老式的好方法)转储完整的sql查询?

So how do I get a dump of a full sql query like this (the good old fashioned way)?

select * from `table_1` where `field_1` = 'xyz' limit 1

谢谢

推荐答案

将此添加到您的路由文件夹中:

Add this in your routes Folder :

\Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
    Log::info( json_encode($query->sql) );
    Log::info( json_encode($query->bindings) );
    Log::info( json_encode($query->time)   );
});

Log :: info()==>会将SQL查询记录在您的storage/logs/laravel.log文件中

Log::info() ==> will log the SQL query in your storage/logs/laravel.log file

var_dump()==>将显示在API调用输出中

var_dump() ==> will display in the API call output

这篇关于Laravel 5雄辩:如何获取正在执行的原始sql? (具有绑定的数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆