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:
hasMany
relationship) which belongs to the authenticated user.So I used an
orWhereHas
method like so:This works fine, however the problem came when I needed to add more filtering logic specific to the
Organisation
resource. I noticed that any filter I would apply after theorWhereHas
wouldn'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 theorWhereHas
method.It finally worked as I intended when I had placed the
whereHas
method as the first filter and hadorWhere
method 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
whereHas
method, 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