Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[12.x] Ensure related models is iterable on HasRelationships@relationLoaded() #55519

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

Merged
merged 2 commits into from
Apr 23, 2025

Conversation

rodrigopedra
Copy link
Contributor

@rodrigopedra rodrigopedra commented Apr 22, 2025

Closes #55518

PR #55471 introduced changes to the HasRelationships@relationLoaded() to improve performance.

It introduced an oversight when loading nesting relations assuming all relations would return am iterable (e.g. Collection). That is the case for some relations, such as HasMany.

But not the case for relations such as BelongsTo, HasOne, MorphOne, which return a single model.

So when iterating one of these relations, the foreach works iterating through the Model instance public properties such as incrementing, exists, ...

This PR:

  • Ensure the relation is iterable before iterating the affected foreach
  • Add a test case

@danharrin
Copy link
Contributor

This does solve #55518, but personally I think you should add a new test case to ensure that this doesn't regress

@rodrigopedra
Copy link
Contributor Author

@danharrin thanks for reviewing it.

I just added a test case

@taylorotwell taylorotwell merged commit 63b4d72 into laravel:12.x Apr 23, 2025
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update to relationLoaded() causes problems when accessing attributes on a model containing a period
5 participants