From 1d5a2e0c3c8e5e2e46d26f061251351af1d53348 Mon Sep 17 00:00:00 2001 From: Lupu Gheorghe Date: Fri, 20 Oct 2023 11:47:23 +0300 Subject: [PATCH] fix filters on project list page --- app/Http/Controllers/ProjectController.php | 1 - app/Http/Filters/ProjectStatusFilter.php | 5 +++-- app/Models/Project.php | 10 ++++++++++ app/Traits/HasProjectStatus.php | 6 ++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 2c0187c8..351b9f01 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -17,7 +17,6 @@ use App\Http\Sorts\ProjectDonationsSumSort; use App\Models\Project; use App\Models\Volunteer; -use Embed\Embed; use Illuminate\Http\Request; use Illuminate\Validation\ValidationException; use Inertia\Inertia; diff --git a/app/Http/Filters/ProjectStatusFilter.php b/app/Http/Filters/ProjectStatusFilter.php index ea0c9d51..0f0f5b09 100644 --- a/app/Http/Filters/ProjectStatusFilter.php +++ b/app/Http/Filters/ProjectStatusFilter.php @@ -11,12 +11,13 @@ class ProjectStatusFilter implements Filter { public function __invoke(Builder $query, $status, string $property): Builder { +// dd($status, $property, $query); if ($status === 'active') { - return $query->isApproved(); + return $query->whereIsOpen(); } if ($status === 'inactive') { - return $query->isRejected(); + return $query->whereIsClosed(); } return $query; diff --git a/app/Models/Project.php b/app/Models/Project.php index cffe43ae..2b6a417e 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -10,6 +10,7 @@ use App\Enums\ProjectStatus; use App\Traits\HasProjectStatus; use Embed\Embed; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -273,4 +274,13 @@ public function getEmbeddedVideosAttribute(): array return $embeddedVideos; } + + public function scopeSearch(Builder $query, ?string $search): Builder { + if (empty($search)) { + return $query; + } + $search = trim($search); + $search = strip_tags($search); + return $query->where('name', 'like', "%{$search}%"); + } } diff --git a/app/Traits/HasProjectStatus.php b/app/Traits/HasProjectStatus.php index f3c53f63..ada46d1e 100644 --- a/app/Traits/HasProjectStatus.php +++ b/app/Traits/HasProjectStatus.php @@ -87,6 +87,12 @@ public function scopeWhereIsOpen(Builder $query): Builder ->whereDate('end', '>=', now()); } + public function scopeWhereIsClosed(Builder $query): Builder + { + return $query->whereIsPublished() + ->whereDate('end', '<', now()); + } + public function scopeWhereStartsSoon(Builder $query): Builder { return $query->whereIsPublished()