beyondcode/laravel-query-detector
Detect N+1 query issues in Laravel during development. Monitors database queries in real time and alerts you when repeated queries suggest missing eager loading, helping you optimize performance and reduce unnecessary database calls.
QueryDetected event), making it non-intrusive to existing architecture. It hooks into the query builder without modifying core Eloquent logic, ensuring backward compatibility.querydetector.php (e.g., whitelisting relations, adjusting thresholds, or disabling in production). Supports environment-specific tuning (e.g., stricter checks in staging).QueryDetected events, enabling custom integrations (e.g., Slack alerts, Datadog metrics, or automated PR comments via GitHub API). Example:
Event::listen(\BeyondCode\QueryDetector\Events\QueryDetected::class, function ($event) {
report(new \App\Events\NPlusOneQueryDetected($event->query));
});
| Risk | Mitigation Strategy | Impact |
|---|---|---|
| False Positives | Configurable threshold (default: 1) and whitelisting reduce noise. |
Low (tunable via .env). |
| Performance Overhead | Only active in debug mode; minimal runtime cost (sub-millisecond per request). |
Negligible. |
| Debugbar Dependency | Optional; fallback to logs/console if Debugbar isn’t installed. | Medium (but non-blocking). |
| Laravel Version Lock | Actively maintained for Laravel 5.8–12.x; backward-compatible via composer. | Low (supports legacy and modern). |
| Custom Outputs | Requires basic PHP knowledge to extend (e.g., creating a new Output class). |
Medium (documented in README). |
Adoption Scope:
Output Strategy:
Threshold Tuning:
threshold=5 for high-traffic APIs).Integration with Observability:
QueryDetected events feed into existing tools (e.g., Sentry, Datadog, or custom dashboards)?Production Readiness:
Legacy Code Impact:
Pilot Phase (1–2 Sprints):
composer require --dev).Configuration Tuning:
threshold and except in querydetector.php.User::posts() if cached).Output Optimization:
Automation (Optional):
php artisan query:detect --fail-on=5).Production Rollout:
production (default) or enable with higher thresholds.| Component | Compatibility | Notes |
|---|---|---|
| Laravel Versions | 5.8–12.x (tested) | Uses feature flags for version support. |
| PHP Versions | 7.4–8.2 (via Laravel support) | No direct PHP constraints. |
| Debugbar | v3–v4 (with runtime resolution) | Fallback to logs if missing. |
| Clockwork | Supported (requires itsgoingd/clockwork) |
Optional dependency. |
| Lumen | Explicit support via LumenQueryDetectorServiceProvider |
Lightweight integration. |
| Custom Outputs | Extendable via Output interface |
Requires ~10 lines of PHP. |
Pre-Reqs:
Installation:
composer require beyondcode/laravel-query-detector --dev
php artisan vendor:publish --provider="BeyondCode\QueryDetector\QueryDetectorServiceProvider"
Configuration:
threshold (e.g., QUERY_DETECTOR_THRESHOLD=3).config/querydetector.php.Testing:
User::with('posts')->get() without eager loading).Scaling:
config/querydetector.php; easy to audit.QUERY_DETECTOR_THRESHOLD).How can I help you explore Laravel packages today?