A global scope automatically modifies every query for a model.
Example:
class ActiveUserScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where('active', true);
}
}
Then register it on the model.
Every normal query becomes effectively:
WHERE active = true
Scenario
Global scopes are useful for things such as tenant isolation or automatically filtering records.
However, I use them carefully because developers may forget that the condition exists.
A query can remove a global scope when appropriate:
User::withoutGlobalScopes()->get();
0 comments:
Post a Comment
Thanks