From cb94315471b02ba012510e99583198124ee00cab Mon Sep 17 00:00:00 2001 From: Philip Iezzi Date: Fri, 30 Aug 2024 18:39:43 +0200 Subject: [PATCH 1/2] Allow banned() scope to be used for both banned and unbanned queries, so it can be used for scope filters (e.g. spatie/laravel-query-builder) --- src/Traits/Bannable.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Traits/Bannable.php b/src/Traits/Bannable.php index 6b57c9b..9a98a23 100644 --- a/src/Traits/Bannable.php +++ b/src/Traits/Bannable.php @@ -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); } /** From 6eed961ad7c724b03120226436ebed45705ef03a Mon Sep 17 00:00:00 2001 From: Philip Iezzi Date: Fri, 20 Sep 2024 18:19:22 +0200 Subject: [PATCH 2/2] Added README note about `banned` flag usage on `banned()` scope to inverse its meaning --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 14b6a22..ee7071b 100644 --- a/README.md +++ b/README.md @@ -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(); ```