jeysonlesmes/laravel-query-detector-with-ajax-requests

dev-master 2018-08-23 15:11 UTC

This package is not auto-updated.

Last update: 2024-09-23 07:26:42 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

Laravel N+1 查询检测器可以帮助您通过减少执行的查询数量来提高应用程序的性能。此包实时监控您的查询,并在开发应用程序时通知您何时应该添加预加载(N+1 查询)。

Example alert

安装

您可以通过composer安装此包

composer require beyondcode/laravel-query-detector --dev

该包将自动注册自己。

使用方法

如果您以debug模式运行应用程序,查询监视器将自动激活。因此,您不需要做任何事情。

默认情况下,此包将显示一个alert()消息来通知您当前请求中发现的N+1查询。

如果您希望此信息写入您的laravel.log文件,写入浏览器控制台日志作为警告或在新的标签页中列出Laravel Debugbar (barryvdh/laravel-debugbar),您可以通过发布配置并更改输出行为来实现(请参阅下面的示例)。

您可以使用以下命令发布包的配置

php artisan vendor:publish --provider=BeyondCode\\QueryDetector\\QueryDetectorServiceProvider

这将添加到您的配置目录中的querydetector.php文件,内容如下

<?php

return [
    /*
     * Enable or disable the query detection.
     * If this is set to "null", the app.debug config value will be used.
     */
    'enabled' => env('QUERY_DETECTOR_ENABLED', null),

    /*
     * Threshold level for the N+1 query detection. If a relation query will be
     * executed more then this amount, the detector will notify you about it.
     */
    'threshold' => 1,

    /*
     * Here you can whitelist model relations.
     *
     * Right now, you need to define the model relation both as the class name and the attribute name on the model.
     * So if an "Author" model would have a "posts" relation that points to a "Post" class, you need to add both
     * the "posts" attribute and the "Post::class", since the relation can get resolved in multiple ways.
     */
    'except' => [
        //Author::class => [
        //    Post::class,
        //    'posts',
        //]
    ],

    /*
     * Define the output format that you want to use. Multiple classes are supported.
     * Available options are:
     *
     * Alert:
     * Displays an alert on the website
     * \BeyondCode\QueryDetector\Outputs\Alert::class
     *
     * Console:
     * Writes the N+1 queries into your browsers console log
     * \BeyondCode\QueryDetector\Outputs\Console::class
     *
     * Clockwork: (make sure you have the itsgoingd/clockwork package installed)
     * Writes the N+1 queries warnings to Clockwork log
     * \BeyondCode\QueryDetector\Outputs\Clockwork::class
     *
     * Debugbar: (make sure you have the barryvdh/laravel-debugbar package installed)
     * Writes the N+1 queries into a custom messages collector of Debugbar
     * \BeyondCode\QueryDetector\Outputs\Debugbar::class
     *
     * JSON:
     * Writes the N+1 queries into the response body of your JSON responses
     * \BeyondCode\QueryDetector\Outputs\Json::class
     *
     * Log:
     * Writes the N+1 queries into the Laravel.log file
     * \BeyondCode\QueryDetector\Outputs\Log::class
     */
    'output' => [
        \BeyondCode\QueryDetector\Outputs\Log::class,
        \BeyondCode\QueryDetector\Outputs\Alert::class,
    ]

];

与ajax请求的集成

为了与ajax请求进行集成,您必须在.env文件中添加变量:QUERY_DETECTOR_AJAX

QUERY_DETECTOR_AJAX=true

除了这一点之外,您还必须在项目中全局添加以下JavaScript代码以显示警告。您可以暂时将其添加或导入到项目的blade布局文件中。

$(document).ajaxComplete(function(event, response){
    if (typeof response.responseJSON.laravelQueryDetector != 'undefined') {
        alert(response.responseJSON.laravelQueryDetector);
    }
});

请确保您的ajax请求的内容类型为:application/json,并且dataType等于json,如下所示

$.ajax({
    url: 'your-url',
    dataType: 'json'
});

如果您使用Lumen,您需要手动复制配置文件并在bootstrap/app.php文件中注册Lumen Service Provider

$app->register(\BeyondCode\QueryDetector\LumenQueryDetectorServiceProvider::class);

如果您需要在包检测到未优化的查询时运行附加逻辑,您可以监听\BeyondCode\QueryDetector\Events\QueryDetected事件并编写一个监听器来运行您自己的处理器。(例如,向Sentry/Bugsnag发送警告,发送Slack通知等。)

测试

composer test

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件marcel@beyondco.de联系,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。