diff --git a/docs/usage/advanced-usage.md b/docs/usage/advanced-usage.md index 4edcb6c..e98be7b 100644 --- a/docs/usage/advanced-usage.md +++ b/docs/usage/advanced-usage.md @@ -7,17 +7,19 @@ Finally you can use our internal OrScope directly. This can be useful for all ca you already have multiple scope classes that you want to combine with an or. ```php +use Lacodix\LaravelGlobalOrScope\Scopes\OrScope; + $orScope = new OrScope([new Scope1()), new Scope2())]) $query->withGlobalScope('or_scope', $orScope); ``` -This is exactly what our `withGlobalOrScopes` on the query builder does. But with that in mind +This is exactly what our `withGlobalOrScopes` method on the query builder does. But with that in mind you can even apply more complex scope combinations like this: ```php $orScope1 = new OrScope([new Scope1()), new Scope2())]) -$orScope2 = new OrScope([new Scope31()), new Scope4())]) +$orScope2 = new OrScope([new Scope3()), new Scope4())]) $query->withGlobalScope('or_scope1', $orScope1); $query->withGlobalScope('or_scope2', $orScope2); @@ -30,7 +32,9 @@ This will result in a query applying ### Scope exchange -Sometimes you already have applied one Scope to your model like this on the classic way: +Sometimes you already have applied one Scope to your model on the classic way: +But for some cases in your application you need this scope combined with another by or condition. +Just do this: ```php class Post extends Model @@ -43,13 +47,10 @@ class Post extends Model } } ``` - -But for some cases in your application you need this scope combined with another by or condition. -Just do this: - +First we remove the solo Scope1::class and re add it in the second step, combined with a new one via OrScope. ```php Post::query() ->withoutGlobalScope(Scope1::class) ->withGlobalOrScopes([new Scope1(), new Scope2()]); ``` -First we remove the solo Scope1::class and re add it in the second step, combined with a new one via OrScope. + diff --git a/docs/usage/disable-scopes.md b/docs/usage/disable-scopes.md index bcde129..af7e48d 100644 --- a/docs/usage/disable-scopes.md +++ b/docs/usage/disable-scopes.md @@ -35,5 +35,5 @@ Post::query()->removedOrScopes(); If you want to disable all global scopes for the whole request, just call ```php -Post::clearGlobalOrScope(); +Post::clearGlobalOrScopes(); ``` diff --git a/docs/usage/register-scopes.md b/docs/usage/register-scopes.md index d6f01e7..1a09ab3 100644 --- a/docs/usage/register-scopes.md +++ b/docs/usage/register-scopes.md @@ -3,14 +3,14 @@ title: Register Global Or Scopes weight: 1 --- -There are multiple ways of registering global scopes that use or condition. +There are multiple ways of registering global scopes that shall use or conditions. ## addGlobalOrScopes The most common use case is registering a global scope on booting the model. Therefor just add our Trait to your model and finally add the global scopes to your model. -ATTENTION: to make our trait work, the global scopes must be registered before the +> ATTENTION: to make our trait work, the global scopes must be registered before the boot()-method of the base Eloquent model is called. The easy way is using the booting-method of your model. @@ -26,7 +26,7 @@ class Post extends Model } ``` -But you can also go with the boot method but be sure to run parent::boot() +But you can also go with the boot method if you ensure to run parent::boot() after registering the global scopes. ```php @@ -45,7 +45,7 @@ class Post extends Model ## addGlobalOrScope -You can also add one single scope, what doesn't make really sense. But maybe +You can also add one single scope, what doesn't make really sense. But if you want to use a loop for registering it might be the correct way. ```php @@ -97,15 +97,15 @@ class Post extends Model ## withGlobalOrScopes -You can also add global scopes with or condition on the fly with using the query builder. +You can also add global scopes with or condition on the fly by using the query builder. In this case you even don't need the GlobalOrScope trait. You can just apply scopes on -every query: +every model query: ```php Post::query()->withGlobalOrScopes([new Scope1, new Scope2]); ``` -Attention: like in laravels base functionality (withGlobalScope) you need to add initialized +> ATTENTION: like in laravels base functionality (withGlobalScope) you need to add initialized scopes, not only classnames. ## Combining with normal global scopes diff --git a/src/Traits/GlobalOrScope.php b/src/Traits/GlobalOrScope.php index 239f156..3b95303 100644 --- a/src/Traits/GlobalOrScope.php +++ b/src/Traits/GlobalOrScope.php @@ -21,7 +21,7 @@ public static function bootGlobalOrScope(): void } } - public static function clearGlobalOrScope(): void + public static function clearGlobalOrScopes(): void { static::$globalOrScopes = []; } diff --git a/tests/Unit/GlobalOrScopesTest.php b/tests/Unit/GlobalOrScopesTest.php index 2c3c250..0c7a407 100644 --- a/tests/Unit/GlobalOrScopesTest.php +++ b/tests/Unit/GlobalOrScopesTest.php @@ -120,7 +120,7 @@ class EloquentQueryGlobalOrScopesTestModel extends Model public static function booting(): void { - static::clearGlobalOrScope(); + static::clearGlobalOrScopes(); } } @@ -132,7 +132,7 @@ class EloquentGlobalOrScopesTestModel extends Model public static function booting(): void { - static::clearGlobalOrScope(); + static::clearGlobalOrScopes(); } public static function boot() @@ -151,7 +151,7 @@ class EloquentClassNameGlobalOrScopesTestModel extends Model public static function booting(): void { - static::clearGlobalOrScope(); + static::clearGlobalOrScopes(); } public static function boot() @@ -170,7 +170,7 @@ class EloquentClosureGlobalOrScopesTestModel extends Model public static function booting(): void { - static::clearGlobalOrScope(); + static::clearGlobalOrScopes(); } public static function boot(): void @@ -205,7 +205,7 @@ class EloquentGlobalOrScopesArrayTestModel extends Model public static function booting(): void { - static::clearGlobalOrScope(); + static::clearGlobalOrScopes(); } public static function boot()