|
| 1 | +# Laravel N+1 Query Detector |
| 2 | + |
| 3 | +[](https://packagist.org/packages/beyondcode/laravel-query-detector) |
| 4 | +[](https://travis-ci.org/beyondcode/laravel-query-detector) |
| 5 | +[](https://scrutinizer-ci.com/g/beyondcode/laravel-query-detector) |
| 6 | +[](https://packagist.org/packages/beyondcode/laravel-query-detector) |
| 7 | + |
| 8 | +The Laravel N+1 query detector helps you increase your application's performance by reducing the number of queries it executed. |
| 9 | +This package will monitor your queries while you develop your application and notify you when you should add eager loading (N+1 queries). |
| 10 | + |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +You can install the package via composer: |
| 15 | + |
| 16 | +```bash |
| 17 | +composer require beyondcode/laravel-query-detector --dev |
| 18 | +``` |
| 19 | + |
| 20 | +The package will automatically register itself. |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +The query monitor will be automatically activated, if you are in debug mode. |
| 25 | + |
| 26 | +By default, this package will display an alert message to notify you about found N+1 queries in the current request. |
| 27 | +If you rather want this information to be written to your `laravel.log` file, you can publish the configuration and change the output behaviour. |
| 28 | + |
| 29 | +You can publish the package's configuration using this command: |
| 30 | + |
| 31 | +```bash |
| 32 | +php artisan vendor:publish --provider=BeyondCode\\QueryDetector\\QueryDetectorServiceProvider |
| 33 | +``` |
| 34 | + |
| 35 | +This will publish the `querydetector.php` file in your config directory with the following contents: |
| 36 | + |
| 37 | +```php |
| 38 | +<?php |
| 39 | + |
| 40 | +return [ |
| 41 | + /* |
| 42 | + * Enable or disable the query detection. |
| 43 | + * If this is set to "null", the app.debug config value will be used. |
| 44 | + */ |
| 45 | + 'enabled' => env('QUERY_DETECTOR_ENABLED', null), |
| 46 | + |
| 47 | + /* |
| 48 | + * Threshold level for the N+1 query detection. If a relation query will be |
| 49 | + * executed more then this amount, the detector will notify you about it. |
| 50 | + */ |
| 51 | + 'threshold' => 1, |
| 52 | + |
| 53 | + /* |
| 54 | + * Here you can whitelist model relations. |
| 55 | + * |
| 56 | + * Right now, you need to define the model relation both as the class name and the attribute name on the model. |
| 57 | + * So if an "Author" model would have a "posts" relation that points to a "Post" class, you need to add both |
| 58 | + * the "posts" attribute and the "Post::class", since the relation can get resolved in multiple ways. |
| 59 | + */ |
| 60 | + 'except' => [ |
| 61 | + //Author::class => [ |
| 62 | + // Post::class, |
| 63 | + // 'posts', |
| 64 | + //] |
| 65 | + ], |
| 66 | + |
| 67 | + /* |
| 68 | + * Define the output format that you want to use. |
| 69 | + * Available options are: |
| 70 | + * |
| 71 | + * Alert: |
| 72 | + * Displays an alert on the website |
| 73 | + * \BeyondCode\QueryDetector\Outputs\Alert::class |
| 74 | + * |
| 75 | + * Log: |
| 76 | + * Writes the N+1 queries into the Laravel.log file |
| 77 | + * \BeyondCode\QueryDetector\Outputs\Log::class |
| 78 | + */ |
| 79 | + 'output' => \BeyondCode\QueryDetector\Outputs\Alert::class, |
| 80 | + |
| 81 | +]; |
| 82 | +``` |
| 83 | + |
| 84 | +### Testing |
| 85 | + |
| 86 | +``` bash |
| 87 | +composer test |
| 88 | +``` |
| 89 | + |
| 90 | +### Changelog |
| 91 | + |
| 92 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 93 | + |
| 94 | +## Contributing |
| 95 | + |
| 96 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 97 | + |
| 98 | +### Security |
| 99 | + |
| 100 | +If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
| 101 | + |
| 102 | +## Credits |
| 103 | + |
| 104 | +- [Marcel Pociot](https://github.com/mpociot) |
| 105 | +- [All Contributors](../../contributors) |
| 106 | + |
| 107 | +## License |
| 108 | + |
| 109 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments