Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Query Detector Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • High Fit: The package is Laravel-native and integrates seamlessly with Eloquent, Laravel’s query builder, and the request lifecycle. It leverages Laravel’s debug mode, service container, and event system, making it a low-friction addition to existing architectures.
  • Performance Monitoring: Aligns with observability-first and SRE-driven architectures by surfacing query inefficiencies without requiring manual instrumentation.
  • Modular Design: Supports multiple output formats (alerts, logs, Debugbar, Clockwork, JSON), allowing integration into existing monitoring stacks (e.g., Datadog, New Relic) via custom event listeners.
  • Non-Invasive: Operates in development mode only (configurable), avoiding production overhead.

Integration Feasibility

  • Zero Configuration for Basic Use: Works out-of-the-box in debug mode with default settings (alerts).
  • Minimal Boilerplate: Requires one Composer command (composer require beyondcode/laravel-query-detector --dev) and no manual setup for core functionality.
  • Lumen Support: Explicitly compatible with Lumen, reducing barriers for micro-service architectures.
  • Event-Driven Extensibility: Emits \BeyondCode\QueryDetector\Events\QueryDetected, enabling custom integrations (e.g., Slack alerts, Sentry logging) without modifying the package.
  • Output Flexibility: Supports 6+ output formats, including:
    • Debugbar (for IDE/debugging workflows)
    • JSON (for API-driven monitoring)
    • Log (for centralized logging)
    • Console (for frontend debugging)

Technical Risk

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.

Key Questions for TPM

  1. Monitoring Stack Alignment:

    • Does the team use Debugbar, Clockwork, or custom logging? If so, which output format should be prioritized?
    • Should N+1 queries trigger external alerts (e.g., Slack, PagerDuty) via custom event listeners?
  2. Production Impact:

    • Should the package be enabled in staging for pre-deployment validation? If so, how will false positives be handled?
  3. CI/CD Integration:

    • Should N+1 queries block PR merges (e.g., via GitHub Actions)? If yes, what threshold (e.g., ">5 N+1 queries") should fail builds?
  4. Legacy System Compatibility:

    • Are there dynamic relations (e.g., polymorphic, conditional) that should be whitelisted to avoid noise?
  5. Scaling Considerations:

    • For high-traffic APIs, should the package sample requests (e.g., 1 in 10) to reduce overhead?
  6. Developer Adoption:

    • Should onboarding docs include a step-by-step optimization guide (e.g., "How to Fix N+1 Queries in 5 Minutes")?

Integration Approach

Stack Fit

  • Laravel Core: Fully compatible with Eloquent, Query Builder, and Laravel’s request lifecycle.
  • Debug Tools: Integrates with Debugbar, Clockwork, and Laravel’s built-in logging.
  • Event System: Leverages Laravel’s event listeners for extensibility.
  • Environment-Aware: Respects APP_DEBUG and can be disabled in production.

Migration Path

  1. Pilot Phase (1–2 Sprints):

    • Install in dev/staging (composer require beyondcode/laravel-query-detector --dev).
    • Enable default alert output and monitor for false positives.
    • Whitelist known safe relations in config/except.
  2. Customization (2–3 Sprints):

    • Publish config (php artisan vendor:publish --provider="BeyondCode\QueryDetector\QueryDetectorServiceProvider").
    • Configure preferred outputs (e.g., Debugbar + Log).
    • Implement custom event listeners for alerts (e.g., Slack, Sentry).
  3. CI/CD Integration (1 Sprint):

    • Add a GitHub Action to scan for N+1 queries in PRs (e.g., fail if >X queries detected).
    • Example:
      - name: Check for N+1 Queries
        run: php artisan query-detector:check --max=5
      
  4. Production Validation (1 Sprint):

    • Enable in staging with QUERY_DETECTOR_ENABLED=true.
    • Verify no false positives before disabling in production.

Compatibility

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.

Sequencing

  1. Phase 1: Detection

    • Enable alerts in development.
    • Identify top 5–10 N+1 hotspots in the codebase.
  2. Phase 2: Optimization

    • Fix queries via eager loading, caching, or batching.
    • Example:
      // 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();
      
  3. Phase 3: Automation

    • Add CI checks to block N+1 queries in PRs.
    • Integrate with monitoring tools (e.g., Datadog dashboards).
  4. Phase 4: Scaling

    • Extend to microservices or high-traffic APIs.
    • Consider sampling in staging to reduce overhead.

Operational Impact

Maintenance

  • Low Effort:
    • No runtime maintenance in production (disabled by default).
    • Dev-only configuration: Updates only required when Laravel versions change.
  • Dependency Management:
    • Monitor for Laravel major version updates (e.g., L13+ compatibility).
    • Watch for Debugbar/Clockwork breaking changes.
  • False Positive Handling:
    • Periodically review whitelisted relations (config/except) as the codebase evolves.

Support

  • Developer Onboarding:
    • 5-minute setup for basic alerts.
    • 1-hour deep dive for custom outputs/event listeners.
  • Troubleshooting:
    • Common issues:
      • False positives: Adjust threshold or whitelist relations.
      • Debugbar conflicts: Ensure compatible version (v3/v4).
      • JSON pollution: Restrict output to dev environments.
    • Debugging Tools:
      • Use config/debugbar or config/console for visibility.
      • Check laravel.log for QueryDetected events.

Scaling

  • Performance:
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata