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.
composer require beyondcode/laravel-query-detector --dev) and no manual setup for core functionality.\BeyondCode\QueryDetector\Events\QueryDetected, enabling custom integrations (e.g., Slack alerts, Sentry logging) without modifying the package.| Risk Area | Assessment | Mitigation |
|---|---|---|
| False Positives | May flag legitimate queries (e.g., dynamic relations) as N+1. | Use whitelisting (config/except) and adjustable thresholds. |
| Performance Overhead | Minimal in dev; none in production (disabled by default). | Disable in production via APP_DEBUG=false or QUERY_DETECTOR_ENABLED=false. |
| Debugbar Dependency | Some outputs (e.g., Debugbar) require additional packages. | Document dependencies clearly; provide fallback outputs (e.g., Log). |
| Backtrace Accuracy | May misattribute queries in complex middleware/queues. | Use custom event listeners to filter false positives. |
| Laravel Version Lock | Supports Laravel 5.8–12.x; may lag behind Laravel 13+. | Monitor upstream compatibility or fork if needed. |
| API Response Pollution | JSON output adds metadata to API responses. | Restrict to non-production environments or use whitelisted routes. |
Monitoring Stack Alignment:
Production Impact:
CI/CD Integration:
Legacy System Compatibility:
Scaling Considerations:
Developer Adoption:
APP_DEBUG and can be disabled in production.Pilot Phase (1–2 Sprints):
composer require beyondcode/laravel-query-detector --dev).config/except.Customization (2–3 Sprints):
php artisan vendor:publish --provider="BeyondCode\QueryDetector\QueryDetectorServiceProvider").CI/CD Integration (1 Sprint):
- name: Check for N+1 Queries
run: php artisan query-detector:check --max=5
Production Validation (1 Sprint):
QUERY_DETECTOR_ENABLED=true.| Component | Compatibility | Notes |
|---|---|---|
| Laravel 5.8–12.x | ✅ Fully supported | Tested up to Laravel 12; monitor for L13+ updates. |
| Lumen | ✅ Supported | Requires manual provider registration. |
| Debugbar | ✅ Supported (v3/v4) | Uses runtime resolution for compatibility. |
| Clockwork | ✅ Supported | Requires itsgoingd/clockwork. |
| Custom Logging | ✅ Supported | Use \BeyondCode\QueryDetector\Outputs\Log::class. |
| API Responses | ✅ JSON Output | Avoid in production; restrict to dev/staging. |
| Queues/Jobs | ⚠️ Partial | May misattribute queries; use whitelisting. |
Phase 1: Detection
Phase 2: Optimization
// Before (N+1)
$posts = Post::all();
foreach ($posts as $post) {
echo $post->author->name; // N+1 query per post
}
// After (Optimized)
$posts = Post::with('author')->get();
Phase 3: Automation
Phase 4: Scaling
config/except) as the codebase evolves.threshold or whitelist relations.config/debugbar or config/console for visibility.laravel.log for QueryDetected events.How can I help you explore Laravel packages today?