Skip to content

Commit

Permalink
Merge pull request #16 from onlime/improve-banned-scope
Browse files Browse the repository at this point in the history
Improve banned() scope to be used for both banned and unbanned queries
  • Loading branch information
mchev authored Sep 20, 2024
2 parents 21fe8aa + 6eed961 commit 5a792d0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ $bannedTeams = Team::banned()->get();
$notBannedTeams = Team::notBanned()->get();
```

> 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.
Unban

```php
$user->unban();
```
Expand Down
8 changes: 4 additions & 4 deletions src/Traits/Bannable.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public function unban(): void
/**
* Scope a query to include only models that are currently banned.
*/
public function scopeBanned(Builder $query): void
public function scopeBanned(Builder $query, bool $banned = true): void
{
$query->whereHas('bans', function ($query) {
$query->notExpired();
});
$banned
? $query->whereHas('bans', fn ($query) => $query->notExpired())
: $this->scopeNotBanned($query);
}

/**
Expand Down

0 comments on commit 5a792d0

Please sign in to comment.