From 66f4d8c7b3f8b2a24cfeb0ddcb93838a07b3d50b Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Thu, 14 May 2026 10:02:38 -0300 Subject: [PATCH] refactor(posts): use $request->collect() + when() for label filter Same semantics, more idiomatic Laravel. Drops the (array) cast, the array_values+array_filter pair, and the if (!empty(...)) guard in favor of $request->collect() + Collection pipeline + $query->when() conditional clause. --- app/Http/Controllers/App/PostController.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/App/PostController.php b/app/Http/Controllers/App/PostController.php index 1d6ef675..8c433123 100644 --- a/app/Http/Controllers/App/PostController.php +++ b/app/Http/Controllers/App/PostController.php @@ -57,13 +57,15 @@ public function index(Request $request, ?string $status = null): Response|Redire $query->where('content', 'ilike', "%{$search}%"); } - $labelIds = array_values(array_filter( - (array) $request->input('labels', []), - fn ($id) => is_string($id) && $id !== '', + $labelIds = $request->collect('labels') + ->filter(fn ($id) => is_string($id) && $id !== '') + ->values() + ->all(); + + $query->when($labelIds, fn ($q) => $q->whereHas( + 'labels', + fn ($q) => $q->whereIn('workspace_labels.id', $labelIds), )); - if (! empty($labelIds)) { - $query->whereHas('labels', fn ($q) => $q->whereIn('workspace_labels.id', $labelIds)); - } return Inertia::render('posts/Index', [ 'workspace' => $workspace,