Closed dijkr closed 1 year ago
This gets the slug, but the products use the ID of its category
$category = basename($request->getRequestUri());
This should point to the slug of the category ID
$products = Product::where('category', $category)->get();
This should work:
$groupedProducts = $products->groupBy('Subcategory');
However, the view outputs the value of the group, it might have changed to the ID, not the actual name of the subcategory:
<h4> {{ $groupedProduct }} </h4>
Solved with this for example:
$product = $products->first();
$categoryData = $product->Category;
Since the array keyname per category contain JSON:
@php
// The resulting key of the array = JSON
$subcategory = json_decode($groupedProduct, true);
@endphp
<div class="grid-item-products-1">
<div class="cat">
<h4> {{ $subcategory['name'] }} </h4>
</div>
</div>
This is from the ProductController:
$products = Product::whereHas('category', function ($query) use ($categorySlug) {
$query->where('slug', $categorySlug); })->get();
$groupedProducts = $products->groupBy('Subcategory');
Right now, the ProductController also use the CategoryController. Please seperate this data. And modify the producten.blade.php accordanly.