Skip to content

[12.x] Improve circular relation check in Automatic Relation Loading #55542

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

Conversation

litvinchuk
Copy link
Contributor

@litvinchuk litvinchuk commented Apr 24, 2025

This PR fixes #55526

In some cases, an infinite loop could occur — for example, when using Chaperone together with Automatic Relation Loading.

We can reproduce infinite loop:

Model::automaticallyEagerLoadRelationships();

$user = User::first();
$post = $user->posts->first();

// make a circular relation
$post->setRelation('user', $user);

// trigger autoload for another relation
dump($user->comments);

Now we have a reference to the shared context, and we can check if it’s the same to prevent an infinite loop.

@litvinchuk litvinchuk marked this pull request as draft April 24, 2025 14:59
@litvinchuk litvinchuk marked this pull request as ready for review April 24, 2025 16:44
@taylorotwell taylorotwell merged commit fef0498 into laravel:12.x Apr 25, 2025
26 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.

Using ->chaperone on model with global automaticallyEagerLoadRelationships causes infinite loop
2 participants