Add whenNested() or nested flag to when() for grouped conditions #58064
Unanswered
musiermoore
asked this question in
Ideas
Replies: 1 comment 2 replies
-
Verbose is better than hidden under the carpet. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Add
whenNested()ornestedflag towhen()for grouped conditionsClosures passed into
when()are currently not grouped automatically.Because of this, mixed
where/orWhereconditions are applied directly to the main query unless an extra nested closure is added.Problem Example
This produces SQL like:
To ensure proper grouping today, you must write:
This becomes verbose and reduces readability.
Proposal
Introduce one of the following (or both, if desired):
1. New whenNested() method
This would automatically apply the callback inside a grouped where, so it would produce clean, grouped SQL:
2. Add a nested parameter to when()
nesteddefaults to false, preserving all existing behavior.When set to true, the callback is executed inside a grouped where, and would also produce:
Summary
Adding
whenNested()or a nested parameter towhen()would simplify grouped conditional queries, prevent subtle logic errors, and improve readability while remaining fully backward compatible.If this sounds useful, I’m happy to create a PR to implement it.
Beta Was this translation helpful? Give feedback.
All reactions