如何在 Laravel 5 中执行查询?DB::getQueryLog() 返回空数组 [英] How to Get the Query Executed in Laravel 5? DB::getQueryLog() Returning Empty Array
问题描述
我正在尝试查看查询日志,但 DB::getQueryLog()
只是返回一个空数组:
$user = User::find(5);print_r(DB::getQueryLog());
结果
数组()
如何查看此查询的日志?
class BeforeAnyDbQueryMiddleware{公共函数句柄($request, Closure $next){DB::enableQueryLog();返回 $next($request);}公共函数终止($request,$response){//存储或转储日志数据...日(数据库::getQueryLog());}}
中间件的链不会针对工匠命令运行,因此对于 CLI 执行,您可以在 artisan.start
事件侦听器中启用查询日志.
例如你可以把它放在bootstrap/app.php
文件中
$app['events']->listen('artisan.start', function(){DB::enableQueryLog();});
3.内存
Laravel 将所有查询保存在内存中.因此,在某些情况下,例如插入大量行时,或者有大量查询的长时间运行的作业时,这可能会导致应用程序使用过多的内存.
在大多数情况下,您只需要在调试时使用查询日志,如果是这种情况,我建议您仅在开发时启用它.
if (App::environment('local')) {//环境是本地的DB::enableQueryLog();}
参考资料
I'm trying to view the log for a query, but DB::getQueryLog()
is just returning an empty array:
$user = User::find(5);
print_r(DB::getQueryLog());
Result
Array
(
)
How can I view the log for this query?
By default, the query log is disabled in Laravel 5: https://github.com/laravel/framework/commit/e0abfe5c49d225567cb4dfd56df9ef05cc297448
You will need to enable the query log by calling:
DB::enableQueryLog();
// and then you can get query log
dd(DB::getQueryLog());
or register an event listener:
DB::listen(
function ($sql, $bindings, $time) {
// $sql - select * from `ncv_users` where `ncv_users`.`id` = ? limit 1
// $bindings - [5]
// $time(in milliseconds) - 0.38
}
);
Some Tips
1. Multiple DB connections
If you have more than one DB connection you must specify which connection to log
To enables query log for my_connection
:
DB::connection('my_connection')->enableQueryLog();
To get query log for my_connection
:
print_r(
DB::connection('my_connection')->getQueryLog()
);
2. Where to enable query log ?
For an HTTP request lifecycle, you can enable query log in the `handle` method of some `BeforeAnyDbQueryMiddleware` [middleware][1] and then retrieve the executed queries in the [`terminate`][2] method of the same middleware.class BeforeAnyDbQueryMiddleware
{
public function handle($request, Closure $next)
{
DB::enableQueryLog();
return $next($request);
}
public function terminate($request, $response)
{
// Store or dump the log data...
dd(
DB::getQueryLog()
);
}
}
A middleware's chain will not run for artisan commands, so for CLI execution you can enable query log in the artisan.start
event listener.
For example you can put it in the bootstrap/app.php
file
$app['events']->listen('artisan.start', function(){
DB::enableQueryLog();
});
3. Memory
Laravel keeps all queries in memory. So in some cases, such as when inserting a large number of rows, or having a long running job with a lot of queries, this can cause the application to use excess memory.
In most cases you will need the query log only for debugging, and if that is the case I would recommend you enable it only for development.
if (App::environment('local')) {
// The environment is local
DB::enableQueryLog();
}
References
这篇关于如何在 Laravel 5 中执行查询?DB::getQueryLog() 返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!