如何在 Laravel 5 中执行查询?DB::getQueryLog() 返回空数组 [英] How to Get the Query Executed in Laravel 5? DB::getQueryLog() Returning Empty Array

查看:52
本文介绍了如何在 Laravel 5 中执行查询?DB::getQueryLog() 返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查看查询日志,但 DB::getQueryLog() 只是返回一个空数组:

$user = User::find(5);print_r(DB::getQueryLog());

结果

数组()

如何查看此查询的日志?

解决方案

Laravel 5 默认禁用查询日志:https://github.com/laravel/framework/commit/e0abfe5c49d2255658cb97ef4df4df4dp497

您需要通过调用启用查询日志:

DB::enableQueryLog();//然后你可以得到查询日志dd(DB::getQueryLog());

或者注册一个事件监听器:

DB::listen(函数($sql,$bindings,$time){//$sql - select * from `ncv_users` where `ncv_users`.`id` = ?限制 1//$bindings - [5]//$time(以毫秒为单位) - 0.38});

一些提示

1.多个数据库连接

如果您有多个数据库连接,则必须指定要记录的连接

要为 my_connection 启用查询日志:

DB::connection('my_connection')->enableQueryLog();

获取my_connection的查询日志:

print_r(DB::connection('my_connection')->getQueryLog());

2.在哪里启用查询日志?

对于 HTTP 请求生命周期,可以在一些 `BeforeAnyDbQueryMiddleware` [middleware][1] 的 `handle` 方法中启用查询日志,然后在同一中间件的 [`terminate`][2] 方法中检索执行的查询.

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屋!

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