Skip to content

Commit d86dd23

Browse files
committed
Enhance allProducts method in ProductListController to support color filtering
This commit adds functionality to the allProducts method, allowing products to be filtered by color. It introduces a new query condition that processes a comma-separated string of color values, improving the product listing experience by enabling users to view products based on their selected colors.
1 parent e9e3406 commit d86dd23

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
@@ -71,6 +71,17 @@ public function allProducts(Request $request){
7171
});
7272
}
7373

74+
if ($request->has('colors')) {
75+
$colorsString = $request->input('colors');
76+
$colorNames = explode(',', $colorsString);
77+
78+
$query->whereHas('variations', function (Builder $variationQuery) use ($colorNames) {
79+
$variationQuery->whereHas('color', function (Builder $colorQuery) use ($colorNames) {
80+
$colorQuery->whereIn('value', $colorNames);
81+
});
82+
});
83+
}
84+
7485
$products = $query->paginate(6);
7586

7687
return ProductResource::collection($products);

0 commit comments

Comments
 (0)