Skip to content

Commit 5a792d0

Browse files
authored
Merge pull request #16 from onlime/improve-banned-scope
Improve banned() scope to be used for both banned and unbanned queries
2 parents 21fe8aa + 6eed961 commit 5a792d0

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ $bannedTeams = Team::banned()->get();
146146
$notBannedTeams = Team::notBanned()->get();
147147
```
148148

149+
> Alternatively to `notBanned()` you may also use the `banned()` scope to filter not-banned models: `Team::banned(false)`. Like this, you could simply use the `banned` scope for e.g. [spatie/laravel-query-builder](https://spatie.be/docs/laravel-query-builder/v5/features/filtering#content-scope-filters) [Scope Filters](https://spatie.be/docs/laravel-query-builder/v5/features/filtering#content-scope-filters), instead of using more complex ways to apply either `banned` or `notBanned` scopes.
150+
149151
Unban
152+
150153
```php
151154
$user->unban();
152155
```

src/Traits/Bannable.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ public function unban(): void
6363
/**
6464
* Scope a query to include only models that are currently banned.
6565
*/
66-
public function scopeBanned(Builder $query): void
66+
public function scopeBanned(Builder $query, bool $banned = true): void
6767
{
68-
$query->whereHas('bans', function ($query) {
69-
$query->notExpired();
70-
});
68+
$banned
69+
? $query->whereHas('bans', fn ($query) => $query->notExpired())
70+
: $this->scopeNotBanned($query);
7171
}
7272

7373
/**

0 commit comments

Comments
 (0)