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.
BREAKING CHANGE: Support for Laravel 8/9/10. Drops support for Laravel 5/6/7.
Based on ajcastro's PR to laravel/framework which was not accepted, sadly.
In this PR we override the
newBelongsToMany()
method of Laravel'sHasRelationships
trait and replace the BelongsToMany class with our own version with support for eager loading pivot relations.Overriding
BelongsToMany's get()
method is not exactly the cleanest solution, but it allows us to tack on support only for BelongsToMany relationships and gives access to the$accessor
property in a clean fashion. In the previous version, some code was dedicated to discovering the relationship model's$accessor
value from the query builder. IMO this is easier to understand, so I understand the original author's decision to go that way. The downside is some duplication of framework code in the overridenget()
method now that we have to do this outside of the framework itself.I suggest that in the future, in order to support newer versions of Laravel, backwards compatibility is dropped in favor of a potentially cleaner solution if we find one.
Thank you for the actual work on this @ajcastro :-)
Minimum PHP version bumped to 7.3 to match Laravel 8's minimum. orchestra/testbench bumped to v6 to match Laravel 8.
Tested with a Laravel 10 application on PHP 8.2.
Close #8
Close #12