Skip to content

Commit 8ebd20e

Browse files
committed
Enhance allProducts method in ProductListController to support category filtering
This commit adds functionality to the allProducts method, allowing products to be filtered by category. It ensures that the category IDs are processed correctly, accommodating both single and multiple category selections. This improvement enhances the product listing experience by enabling users to view products based on their selected categories.
1 parent 68ec5d0 commit 8ebd20e

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

app/Http/Controllers/Product/ProductListController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ 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+
72+
$query->whereHas('categories', function (Builder $categoryQuery) use ($categoryIds) {
73+
$categoryQuery->whereIn('id', $categoryIds);
74+
});
75+
}
6576

6677
$products = $query->paginate(6);
6778

0 commit comments

Comments
 (0)