php Laravel 5 Eloquent:如何获取正在执行的原始 sql?(带有绑定数据)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39270169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:50:35  来源:igfitidea点击:

Laravel 5 Eloquent: How to get raw sql that is being executed? (with binded data)

phpmysqllaravel-5

提问by Raheel Hasan

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:

我试图弄清楚如何获取正在执行的原始 sql 查询,包括其中的绑定数据。这是我得到的:

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

And here is what the result looks like:

结果如下:

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

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

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

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

Thanks

谢谢

回答by zeetit

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() ==> will log the SQL query in your storage/logs/laravel.log file

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

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

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

回答by snipe

You may want to check out the Laravel debugbar. I wouldn't develop a Laravel app without it. It will give you a breakdown of all queries (including repeated queries and ajax queries), with the speed they executed at and a note to the line in the controller/method that called them. (It provides TONS more info as well, such as views, gates, routes, etc.)

您可能想查看Laravel 调试栏。没有它,我不会开发 Laravel 应用程序。它将为您提供所有查询(包括重复查询和 ajax 查询)的细分,以及它们执行的速度以及调用它们的控制器/方法中的行的注释。(它还提供了 TONS 更多信息,例如视图、大门、路线等)

Also, Laravel has a toSql()method that you can use instead of your example. It will only show you the prepared statement as your example does, but it's at least a little cleaner. If you use toSql(), you have to apply it before you execute the query though.

此外,Laravel 有一种toSql()方法可以用来代替示例。它只会像您的示例那样向您显示准备好的语句,但它至少更简洁一些。如果使用toSql(),则必须在执行查询之前应用它。

$foo = Foo::where('bar', 'baz');
$foo_sql = $foo->toSql();
$foo->get();

回答by Govind Samrow

Try to add event listener for query :

尝试为查询添加事件侦听器:

Event::listen('illuminate.query', function($query)
{
    var_dump($query);
});

or

或者

$results = User::where('id',$id)->toSql();
dd($results)

回答by Dharmik

Add this function to your application and simply call.

将此函数添加到您的应用程序中,只需调用即可。

function getQuery($sql){
        $query = str_replace(array('?'), array('\'%s\''), $sql->toSql());
        $query = vsprintf($query, $sql->getBindings());     
        return $query;
}
$foo = Foo::where('bar', 'baz');
print_r(getQuery($foo));

Output: select * from Foowhere bar= 'baz'

输出:select * from Foowhere bar= 'baz'

回答by Dimo Doncheff

Another option is to get the queries from Laravel Debugbar:

另一种选择是从Laravel Debugbar获取查询:

$queries = debugbar()->getCollector('queries');  
$statements = $queries->collect()['statements'];
dd($statements);