The position of 'whereHas' in a query builder matters, is it intended? #54476
Unanswered
chikondikamwendo
asked this question in
Q&A
Replies: 0 comments
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.
-
Laravel 11.x
PHP 8.3
I recently had the need to query resources based on the following factors:
hasManyrelationship) which belongs to the authenticated user.So I used an
orWhereHasmethod like so:This works fine, however the problem came when I needed to add more filtering logic specific to the
Organisationresource. I noticed that any filter I would apply after theorWhereHaswouldn't take any effect on the result. For example if I need to get the last resource, this will still return the first resource. If I apply another where clause, this won't take any effect on the result it will only consider up to theorWhereHasmethod.It finally worked as I intended when I had placed the
whereHasmethod as the first filter and hadorWheremethod as the second filter, any other subsequent filter I applied after this specific order would take effect on the result like so:This specific ordering of the
whereHasmethod, is this how it is supposed to work? or i'm missing something for it to work the way i wanted it to while using the first way of things (disregarding order)?Beta Was this translation helpful? Give feedback.
All reactions