-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[9.x] Add whereNot() to Query Builder and Eloquent Builder #41096
Conversation
I attempted same thing before and was rejected for no reason #36522 |
I had not seen your PR yet, good you mention it. I thought it was
strange no-one had come up with this before, good to see it turns out
someone had.
I think there is a slight difference between our PR's. While they both
add the whereNot() method, your focus seemed to be to provide support
for ->whereNot('name', '<>', "bar") . It was only later that you mention
the use-case of using whereNot() containing multiple subconditions. I
think your PR was rejected based on your initial post, and after that
not being reconsidered anymore.
As mentioned in this PR, this PR focuses on the whereNot()
implementation for passing closures with subconditions, and does not aim
to support e.g. ->whereNot('name', '<>', "bar") , which in my opinion
would not be that useful. I guess focussing on this method signature may
make a better case for this PR to be considered.
|
I also needed this, so I created a package, but I'd be glad if this got merged into the framework. |
@marcovo First of all i'm happy that yours got merged. You said my PR focused on doing a whereNot on a value but yours on a callback. $query->where(function($query){
$query->...
}) Since my PR was using the existing |
@shadoWalker89 Yeah did read through your code when I wrote my reply, and saw your implementation follows the same lines as mine, albeit my implementation is a limited version of what your version provided. I did not mean to say your code did not support passing closures. What I meant is you did not mention (=focus on) the closure use-case in your initial post. I think that in a pull request, the description is at least as important as the actual code, in order to properly communicate the intentions of the PR. Therefore, I suspect if you had mentioned in your first post the details on the closure usage you did mention in your last post there, your PR may have had a better chance of being merged. In order words, I suspect (but this is purely speculation from my side) the maintainers may often make decisions based on the description alone without looking at the committed code. Therefore it is important the description covers your intents. I suspect only after they decide the described new functionality is useful, they may go and look at the implementing code. (But then again, this is pure speculation.) |
I wish this included support for passing a column/value pair… I always type |
You're always free to propose a pull request of course. Beware though, a naive implementation would amount to a query like Note that within the scope of this pull request precedence is not an issue because closures always add parentheses. |
@marcovo, I understand. I may try to take a look in the coming weeks. |
Summary
From time to time I encounter the situation I want to negate a relatively complicated query builder expression, often hidden away in a (somewhat large) scope. One could start writing the negated version of that scope, or just use
->whereNot(fn ($q) => $q->someScope())
, except that laravel does not currently providewhereNot()
functionality. Until now each time I encountered this I have added this method by defining macros, but I think it is well worth to add this function to laravel itself.This pull request proposes to add
whereNot()
to both the Query Builder and Eloquent Builder. Using this new method, one can now easily add a negated (nested) condition to the query builder, or e.g. define a negated scope in terms of a positive scope:Added functionality
This pull request adds 4 methods:
whereNot()
to the Query BuilderorWhereNot()
to the Query BuilderwhereNot()
to the Eloquent BuilderorWhereNot()
to the Eloquent BuilderI decided to only include support for passing closures as parameters, because
whereNot('column', '=', 'foo')
to me seems inferior to just usingwhere('column', '!=', 'foo')
As far as I can tell, all necessary tests are included.
Backwards compatibility
One minor incompatibility may be introduced by this pull request, which would be if a developer has named a database column
not
and useswhereNot(...)
to add conditions based on this column. I think it is unlikely someone will name a columnnot
as it is not descriptive, so I suppose this incompatibility can be accepted.Considerations
This is my first pull request towards Laravel, I hope everything is alright. A few points of attention:
testNestedWhere()
in that same file, with minor modifications in two places. Should these be generalized using a data provider?Thanks!