- How do I install BeyondCode’s Laravel N+1 Query Detector?
- Run `composer require beyondcode/laravel-query-detector --dev` to install it as a development dependency. The package auto-registers, so no additional steps are needed unless you want to customize its behavior via `php artisan vendor:publish`.
- Does this package work with Laravel 12 and PHP 8.2+?
- Yes, the package officially supports Laravel 5.7–12.x and PHP 8+. Check the [CHANGELOG](https://github.com/beyondcode/laravel-query-detector/blob/main/CHANGELOG.md) for version-specific updates, but it’s actively maintained for modern stacks.
- Will this slow down my production environment?
- No, the package is disabled in production by default (via `QUERY_DETECTOR_ENABLED`). Enable it only in development or staging to avoid runtime overhead. For high-traffic apps, consider sampling (e.g., monitoring 1% of requests).
- How do I ignore specific queries or relationships?
- Use the `except` configuration in `config/query-detector.php` to whitelist queries or relationships. For example, add `'except' => ['User::with('posts')->where(...)']` to skip dynamic or intentional N+1 patterns.
- Can I integrate this with Sentry or Slack for alerts?
- Yes, the package emits a `QueryDetected` event. Subscribe to it in an event listener to forward alerts to Sentry, Slack, or other tools. Example: `event(new QueryDetected($query, $count));` in your listener.
- Does this work with Lumen or non-Eloquent queries?
- The package supports Lumen via `LumenQueryDetectorServiceProvider` and focuses on Eloquent ORM/Query Builder. For raw PDO queries or non-Laravel stacks, you’d need a custom adapter, as it’s Laravel-centric.
- How do I test if N+1 queries are fixed in my CI pipeline?
- Mock the `QueryDetector` in unit tests to verify no N+1 queries exist in critical paths. For integration tests, enable the detector in your test environment and assert no alerts are triggered. Use `QueryDetector::assertNoQueries()` if available.
- What’s the best way to visualize N+1 queries in my app?
- Pair this with `barryvdh/laravel-debugbar` for a visual breakdown of queries. Alternatively, use the console output or log alerts to a file. For APIs, output JSON via a custom listener for frontend teams.
- Are there alternatives to this package?
- Yes, alternatives include `spatie/laravel-query-builder` (for query optimization) or `laravel-debugbar` (for general query logging). However, this package is specialized for real-time N+1 detection with minimal setup, making it ideal for Laravel devs.
- How do I adjust the threshold for N+1 detection?
- Set `QUERY_DETECTOR_THRESHOLD` in your `.env` (default: `1`). For example, `QUERY_DETECTOR_THRESHOLD=3` will alert only if a relationship is queried 3+ times. Start with `1` and refine based on your app’s traffic patterns.