-
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
[5.5] Letting where
in query builder take an array
#19126
Conversation
This functionality is similar to where doing something like `where('id', '=', null)` translates to `whereNull('id')` Now `where('id', [1,2])` or `where('id', '=', [1,2])` will translate to `whereIn('id', [1,2])`. And, like `where` with `null` will map any operator other than `=` to `whereNotNull`, we map it to `whereNotIn`.
where
in query builder take an arraywhere
in query builder take an array
|
Ya I think that'd make sense. I'll patch it today. |
Does this also work with ...
How about those other where-methods that aren't already accepting arrays, like whereDate, whereMonth, whereYear? Note: This functionality will conflict with future development in #18229 if the syntax |
Also... this code overrides/ignores the $operator, and assumes that everyone passing in array/Arrayable really wanted whereIn.
And since ::find($id) just calls This change, in its current form, affects many more methods than expected. |
Okay, so ... I had a major brainfreeze. I forgot about the Eloquent\Builder code. So the Model::find and Model::delete issues are invalid, the issues are about the Database\Builder. Instead of the
On the other hand, that brainfreeze may still be in effect and I am totally missing something. |
@sisve Seems the main concern might be that if you passed an array by mistake when you didn't really want to, the query would still happen, where previously you would get some type of error due to conversion of type that would make it obvious what you passed wasn't what you expected? |
Passing a two dimensional array into where as the first argument gets handled by the top pre-existing conditional in that method. It doesn't override operator, it instead utilizes existing conventions and functionally that occur. I see references in a comment of the PR you linked to a hypothetical API, but that will be up to the implementation. If he implements that API, his conditional is higher, and has a stronger specificity, then this should be fine. We can of course tweak the implementation here to loosely accept any iterable in general as well. As for find, I agree that someone passing it in may do so unintentionally, and I cover that in the Known Concerns section. Sorry for lack of code inlining and my general shortness - writing on a phone half awake 😄 |
I should probably clarify my stance on this issue. My earlier comments are mostly about noticing changes that needs to be documented. I am not against the feature, I just want it properly documented in the changelog/upgrade notes that functions that previously assumed single primary keys now supports arrays. People should already be validating their inputs, but that is not always the case... |
The main issue I find with this is the inherent need to then support the other comparators. Sure, This gets a strong no from me. |
@ConnorVG I'm not sure the need is inherent. I implemented it the current way because it's identical to how Rails has never supported it and I don't think I've seen any Python or other ORMs that'd allow it either. Rails example:
Overall I'd be in favor for not allowing arrays to be passed to So while I understand that concern, I'll point out that this is not creating a new potential issue. It piggybacks on the existing issue Either way I'll ensure it's documented properly under the |
@machuga the problem is that with a primitive understanding of |
Hmm I certainly understand the concern, but not entirely sure I agree with it based on the precedence already set by the ORM and other areas for convenience in Laravel. Either way I'm very happy this discussion is covering multiple viewpoints of the API spreading 👍 |
I think I want to maybe hold off on this for now because I don't feel it's super clear or known how this will affect wider APIs and methods. |
Works for me! |
Description
There was a discussion on internals, laravel/ideas#572,
about adding
in
as an operator forwhere
. I riffed on it and ignored addingin
asa focus and simply mapped values of arrays to be passed along to
whereIn
.This functionality is similar to
where
doing something likewhere('id', '=', null)
translates towhereNull('id')
Now
where('id', [1,2])
orwhere('id', '=', [1,2])
will translate to
whereIn('id', [1,2])
.And, like
where
withnull
will map any operator other than=
to
whereNotNull
, we map it towhereNotIn
.This functionality exists in other ecosystems, which doesn't necessarily warrant that
it should be part of ours, but I had some extra time and decided to make a PR for it
because I haven't made a Laravel PR in forever, and it was something some folk seemed
interested in. Low hanging fruit and what not. As such, I submit it for feedback.
Known Concerns
where
where
clauseswhereIn
could see).where('id', '=', [1,2])
looks silly and incorrect. We allow the same fornull
, but it's not ideal.