如何获取在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());
结果
Array
(
)
如何查看此查询的日志?
默认情况下,Laravel 5中禁用了查询日志: https://github.com/laravel/framework/commit/e0abfe5c49d225567cb4dfd56df9ef05cc297448
您将需要通过调用以下命令来启用查询日志:
DB::enableQueryLog();
或注册事件侦听器:
DB::listen(
function ($sql, $bindings, $time) {
// $sql - select * from `ncv_users` where `ncv_users`.`id` = ? limit 1
// $bindings - [5]
// $time(in milliseconds) - 0.38
}
);
一些提示
1.多个数据库连接
如果您有多个数据库连接,则必须指定要记录的连接
要启用my_connection
的查询日志:
DB::connection('my_connection')->enableQueryLog();
要获取my_connection
的查询日志:
print_r(
DB::connection('my_connection')->getQueryLog()
);
2.在何处启用查询日志?
对于HTTP请求生命周期,您可以在某些BeforeAnyDbQueryMiddleware
中间件,然后在 terminate
方法中检索已执行的查询相同的中间件.
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()
);
}
}
工匠命令不会运行中间件链,因此对于CLI执行,您可以在artisan.start
事件侦听器中启用查询日志.
例如,您可以将其放入bootstrap/app.php
文件
$app['events']->listen('artisan.start', function(){
\DB::enableQueryLog();
});
3.内存
Laravel将所有查询保留在内存中.因此,在某些情况下,例如插入大量行或长时间运行带有大量查询的作业时,这可能导致应用程序使用过多的内存.
在大多数情况下,您仅需要查询日志进行调试,如果是这种情况,我建议您仅将其用于开发.
if (App::environment('local')) {
// The environment is 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();
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 and then retrieve the executed queries in the terminate
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屋!