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

[5.7] Fix BelongsToMany pivot relationship child with loaded relations wakeup #27358

Merged
merged 3 commits into from Jan 30, 2019

Conversation

Projects
None yet
2 participants
@erikgaal
Copy link
Contributor

erikgaal commented Jan 30, 2019

Overview

This PR demonstrates the bug that was reported in #23068. Although a previous PR (#23081) was submitted and merged to fix the issue, another edge case caused the bug to persist.

Reproduction

Whenever a model is serialized that has a loaded pivot relation that is using a Pivot class, and that pivot class has loaded relations, the following exception is thrown:

Illuminate\Database\Eloquent\RelationNotFoundException : Call to undefined relationship [pivot] on model [Illuminate\Tests\Integration\Queue\Role].

The bug persists because in the case that there are loaded relations within the pivot, they are still appended to the result of getQueueableRelations() as relation.pivot.child, even if the relation method pivot does not exist.

foreach ($this->getRelations() as $key => $relation) {
if (method_exists($this, $key)) {
$relations[] = $key;
}
if ($relation instanceof QueueableCollection) {
foreach ($relation->getQueueableRelations() as $collectionValue) {
$relations[] = $key.'.'.$collectionValue;
}
}
if ($relation instanceof QueueableEntity) {
foreach ($relation->getQueueableRelations() as $entityKey => $entityValue) {
$relations[] = $key.'.'.$entityValue;
}
}
}

Proposed Solution

In order to prevent any children from a pivot (or non-existing relationship) to be serialized, the foreach in getQueueableRelations() should continue when the relationship method does not exist.

@taylorotwell taylorotwell merged commit d269f36 into laravel:5.7 Jan 30, 2019

2 checks passed

continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment