[9.x] allow for named global class scopes #36934
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.
This pull request proposes a possibility to assign a name to a global scope defined by a scope class (re-targeted to master for 9.x from #36909).
Right now, it is possible to define a global scope in three different ways:
static::addGlobalScope('nameOfScope', function ($builder) { $builder->where('someColumn', '>', 1); });static::addGlobalScope(function ($builder) { $builder->where('someColumn', '>', 1); });static::addGlocalScope(new MyCustomScope());The first would be named
nameOfScope, the second something like0000000017da6fe1000000003ed5b2bcand the third will go by the fully qualified class name of MyCustomScope.In cases where
MyCustomScopeactually takes parameters, you are therefore not able to apply multiple scopes like:because the scope ordering by birth_date would overwrite the one previously assigned that ordered by name.
Of course it would be possible to call the Scope like
static::addGlocalScope(new OrderByScope(['name', 'desc'], 'birth_date'));and chain the orderBys within the Scope, but then you would not be able to disable one of them selectively in a later query.This PR suggests a change that, additionally to the existing invocations, allows to define a global scope on a Model like this:
The scopes are distinctively named
orderByNameandorderByBirthDate, respectively, which allows for multiple class scopes to be stacked on a model with different parameters and of course to dynamically disable each one of them.