Skip to content

Add @whereDoesntHaveConditions directive - #2829

Open
officialasishkumar wants to merge 1 commit into
nuwave:masterfrom
officialasishkumar:where-doesnt-have-conditions
Open

officialasishkumar wants to merge 1 commit into
nuwave:masterfrom
officialasishkumar:where-doesnt-have-conditions

Conversation

@officialasishkumar

@officialasishkumar officialasishkumar commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #1646

  • Added or updated tests
  • Documented user facing changes

Changes

Adds @whereDoesntHaveConditions, the counterpart of @whereHasConditions that filters a query by the absence of related models matching client given conditions.

type Query {
  people(
    doesntHaveProjects: _ @whereDoesntHaveConditions(columns: ["id", "name"])
  ): [Person!]! @all
}
{
  people(doesntHaveProjects: { column: ID, value: 5 }) {
    name
  }
}

This returns every person who is not related to the project with ID 5, including people without any project.
An empty object returns only models without any related model, null leaves the query unchanged.

The directive extends WhereHasConditionsDirective and supports the same relation, columns, columnsEnum and handler arguments.
It infers the relation from argument names following the convention doesntHave{$RELATION} and generates input types with the suffix WhereDoesntHaveConditions.
It passes the conditions on as a HAS condition with operator: < and amount: 1, which Eloquent turns into NOT EXISTS, just like whereDoesntHave().
With dot notation such as relation: "user.company", it excludes posts whose user has a company that matches the conditions.
Custom handlers receive the same HAS structure that clients could already send through @whereConditions.

To share the logic, WhereHasConditionsDirective gets two small protected hooks for the relation name prefix and the amount operator.

Breaking changes

None.

Changelog

Added

  • Add @whereDoesntHaveConditions directive to filter a query by the absence of related models matching dynamic conditions

`@whereHasConditions` lets clients filter a query by the existence of
related models that match dynamic conditions, but there was no
counterpart to filter by their absence. Excluding records related to a
given model while keeping records without any related models required
letting clients build `HAS` conditions with `operator: LT` and
`amount: 1` through `@whereConditions`, with a client-controlled
relation name.

`@whereDoesntHaveConditions` works like `@whereHasConditions`, but
constrains the relation with `< 1` instead of `>= 1`, which Eloquent
turns into `NOT EXISTS`, just like `whereDoesntHave()`. It supports the
same `relation`, `columns`, `columnsEnum` and `handler` arguments and
infers the relation from argument names following the convention
`doesntHave{$RELATION}`. Passing an empty object filters for models
without any related model, `null` leaves the query unchanged.

Resolves nuwave#1646
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T20:17:04.056453Z f848605 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@officialasishkumar

Copy link
Copy Markdown
Contributor Author

The failing PHPStan on ... and highest dependencies jobs are unrelated to this change.
phpstan/phpstan 2.4.0 was released after the last green run on master and reports 14 method.alreadyNarrowedType errors for assertInstanceOf() in DeferDBTest, PaginatedRelationLoaderTest and SubscriptionTest, which this PR does not touch.
I get the same errors when running PHPStan on master with the latest dependencies.
The runner then crashes while printing PHPStan's output, so those jobs have no logs.
PHPUnit, PHPStan with the lowest dependencies and the benchmarks pass.

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.

Add @whereDoesntHaveConditions directive

1 participant