Skip to content

Add #[SuppressQueryDetection] attribute to suppress N+1 detection per method #115

Closed
dingyaguang117 wants to merge 0 commit intobeyondcode:masterfrom
dingyaguang117:master
Closed

Add #[SuppressQueryDetection] attribute to suppress N+1 detection per method #115
dingyaguang117 wants to merge 0 commit intobeyondcode:masterfrom
dingyaguang117:master

Conversation

@dingyaguang117
Copy link

@dingyaguang117 dingyaguang117 commented Feb 12, 2026

Summary

  • Add a PHP 8 #[SuppressQueryDetection] attribute that can be placed on any method to suppress N+1 query detection for all queries triggered within it (including nested calls)
  • Gracefully degrade on PHP 7.x — the attribute is silently ignored when PHP_VERSION_ID < 80000
  • Add 6 test cases covering: basic suppression, morph relations, multiple relations, nested calls, and coexistence with unsuppressed methods

Motivation

The existing except config requires whitelisting by model + relation pair, which is too coarse-grained. In practice, developers often need to suppress detection for a specific method (e.g. an admin controller action, a background job, or a legacy endpoint) rather than globally whitelisting a
relation.

This attribute-based approach gives fine-grained, code-level control — just annotate the method:

  use BeyondCode\QueryDetector\Attributes\SuppressQueryDetection;

  #[SuppressQueryDetection]
  public function dashboard()
  {
      // N+1 queries here will not be reported
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments