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.
This commit is contained in:
Paulo Castellano 2026-05-14 10:02:38 -03:00
parent af96cb0a0e
commit 66f4d8c7b3

View file

@ -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,