Laravel 5.3-如何在页面上记录所有查询? [英] Laravel 5.3 - How to log all queries on a page?

查看:226
本文介绍了Laravel 5.3-如何在页面上记录所有查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的团队正在从事一个相当大的项目.查询无处不在-控制器中,视图中的视图编辑器(延迟加载)以及其他一些服务中.跟踪所有内容变得越来越困难,当前页面加载速度相当慢.

My team and I are working on a rather big project. There's queries going on everywhere - in controllers, in view composers in views (lazy loading) and probably in some other services as well. It's getting hard to keep a track of it all and the page load speed is fairly slow at the moment.

我将把\ DB :: enableQueryLog()和\ DB :: getQueryLog()记录在哪里,并记录所有查询并将其转储?基本上,我正在寻找代码中发生在任何查询发生之前的某个位置(放置enableQueryLog()),并且正在寻找在视图呈现之后发生的某个位置(以转储getQueryLog()).

Where would I put \DB::enableQueryLog() and \DB::getQueryLog() to log ALL the queries and dump them? Basically I'm looking for some place in code that happens before any of the queries happen (to put enableQueryLog()) and I'm looking for a place that happens after the views render (to dump getQueryLog()).

什么是解决这个问题的好方法?

What would be a good way to go about this?

谢谢.

推荐答案

以下是一个完美的示例:

Here comes the perfect example:

https://laravel.com/docs/5.3/database#监听查询事件

打开 app \ Providers \ AppServiceProvider.php 并将以下内容添加到Boot()函数:

Open app\Providers\AppServiceProvider.php and add the following to Boot() function:

DB::listen(function ($query) {
    var_dump([
        $query->sql,
        $query->bindings,
        $query->time
    ]);
});

这篇关于Laravel 5.3-如何在页面上记录所有查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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