Skip to content

Commit e9e3406

Browse files
committed
Refactor category filtering in allProducts method of ProductListController
This commit updates the allProducts method to change the way category filtering is handled. It replaces the previous 'category_id' input with 'categories', allowing for a comma-separated string of category IDs to be processed. Additionally, the query now correctly references 'categories.id' for filtering, improving the accuracy of product listings based on selected categories.
1 parent 8ebd20e commit e9e3406

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

app/Http/Controllers/Product/ProductListController.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,12 @@ public function allProducts(Request $request){
6262
});
6363
}
6464

65-
if ($request->has('category_id')) {
66-
$categoryIds = $request->input('category_id');
67-
// S'assurer que $categoryIds est un tableau pour éviter des erreurs
68-
if (!is_array($categoryIds)) {
69-
$categoryIds = [$categoryIds];
70-
}
71-
65+
if ($request->has('categories')) {
66+
$categoryIdsString = $request->input('categories');
67+
$categoryIds = explode(',', $categoryIdsString);
68+
7269
$query->whereHas('categories', function (Builder $categoryQuery) use ($categoryIds) {
73-
$categoryQuery->whereIn('id', $categoryIds);
70+
$categoryQuery->whereIn('categories.id', $categoryIds); // <-- Corrected
7471
});
7572
}
7673

0 commit comments

Comments
 (0)