[5.6] Fix for HasManyThrough returning incorrect results with chunk()… #24096
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Laravel Version: 5.6.17
PHP Version: 7.1.3 (MacOS 10.13.4)
Description:
Summary:
This is pull request addresses the issues reported on #21774. Using the
chunk()
method on aHasManyThrough
relation object returns incorrect results. This is due to to the fact that Eloquent QueryBuilder attempts to hydrate the the related model with intermediate model attributes.With the current codebase
get()
method has fixes the issue by getting an instance of the builder with the correct table and columns selected to overcome the issue. This can be demonstrated by the different model structure that would be retrieved when running$country->posts()->get()
vs$country->posts()->getQuery()->get()
The same issue is manifested when using
each()
method. This is becauseeach()
uses the underlyingchunk()
method during its runtime.Proposed fix
chunk()
andeach()
methods were added toHasManyThrough
class to correct the selection of columns before they operate. This would contain the behaviour change toHasManyThrough
without affecting any other behaviours. Hence, there are no breaking changes introduced.Tests
Added two extra tests to
DatabaseEloquentHasManyThroughIntegrationTest
to testchunk()
andeach()
, where the models are checked to have the correct columns defined.