-
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
[Bug] Querying relations with extra conditions not working as expected #1272
Comments
Yeah, the matcher is matching on the foreign key to associate the child models with their parents, so it's losing the ones where you're matching on local_id, since you're essentially matching on two foreign keys at the same time. I think you could accomplish this by defining a separate relationship called "allMatches" and then loading that, though you still wouldn't be able to eager load it. Eager loading is not really helping you in terms of performance in this particular use case anyways as you're only loading matches for one team, but you could do something like this:
Then...
|
Ok I see, I really ended up eager loading them because I didn't know I could add more conditions to the eloquent method and this is the only I could think of to achieve what I wanted. I think it's a bit confusing that some query results are lost, because many times I guide myself dumping the raw query that is called to see what's happening. Thank you for you help, this works great! |
For people interested, I managed to get this to work in reverse. The original wasn't working for me.
|
@taylorotwell For the case where visitant_id matches matches.id and sponsor is not company I still get that record. |
That's really nice. But what if I need to load
|
Solved See on this https://laratuto.com/laravel-eloquent-relations-not-working/ |
have you got answer to use it with eager loading ? |
I have been some time playing with the querying system for eloquent to get something done, and recently I found something that I think is a bug.
I have a table "Teams", with id and name columns. And I have a table "Matches" with local_id and visitant_id. What I want to achieve is to get all the matches a team has played, be it as local or visitant. And finally I come up with this.
On the Teams model:
On the Teams controller:
The query that is run with this is:
After this I expect $team->matches_as_visitant to be actually all the matches, because the eloquent model already matches the visitant_id field and I add a orWhere part where it checks for the local_id. This should work but doesn't, I've been inspecting the framework code and I found at which point this actually "stops working".
In the HasOneOrMany class we have a method matchOneOrMany:
Basically the $results variable is a Collection with all the matches, but then the $models variable (that is then returned) only contains the matches played as visitant, and the local matches are lost.
The text was updated successfully, but these errors were encountered: