[5.2] Refactor relations and scopes #13824
Merged
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.
As discussed on Slack, I refactored some stuff that was recently made overly complex by some pull requests. All in all, I managed to shave off around 100 lines of code and all tests still pass. I didn't touch any public methods so this shouldn't break anything.
Summary of scope refactoring:
applyCallbackToQuery
andcallScope
in a single more general method calledcallScope
mergeModelDefinedRelationWheresToHasQuery
, made it public, and renamed it tomergeModelDefinedRelationConstraints
so it can now be used from relations as well.applyScopes
(which applies global scopes) by utilizing the newcallScope
method. As a result theapplyScope
helper method is not needed any more.getGlobalScope
on the Model to match the logic ofwithoutGlobalScope
on the Builder.Summary of morphTo refactoring:
getEagerLoadsForInstance
was deleted and replaced with a call tonestedRelations
on the Builder (which is now public).mergeRelationWheresToMorphQuery
was deleted as well. The new public Builder methodmergeModelDefinedRelationConstraints
is used instead.Summary of SoftDeletes refactoring:
withTrashed
andonlyTrashed
static helpers from theSoftDeletes
trait, since the call is redirected to query macro anyway.