简体   繁体   English

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

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

Im trying to figure out how to get the raw sql query being executed including the binded data in it.我试图弄清楚如何获取正在执行的原始 sql 查询,包括其中的绑定数据。 Here is what ive got:这是我得到的:

\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谢谢

You may want to check out the Laravel debugbar .您可能想查看Laravel 调试栏 I wouldn't develop a Laravel app without it.没有它,我不会开发 Laravel 应用程序。 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.它将为您提供所有查询(包括重复查询和 ajax 查询)的细分,以及它们执行的速度以及调用它们的控制器/方法中的行的注释。 (It provides TONS more info as well, such as views, gates, routes, etc.) (它还提供了 TONS 更多信息,例如视图、大门、路线等)

Also, Laravel has a toSql() method that you can use instead of your example.此外,Laravel 有一个toSql()方法,您可以使用它来代替示例。 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.如果您使用toSql() ,则必须在执行查询之前应用它。

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

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 调用输出中

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 Foo where bar = 'baz'输出: select * from Foo where bar = 'baz'

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)

Another option is to get the queries from Laravel Debugbar :另一种选择是从Laravel Debugbar获取查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM