dijkr / Copia

Laravel-project with CMS
0 stars 0 forks source link

Transfer the category query from the ProductController to the CategoryController #11

Closed dijkr closed 1 year ago

dijkr commented 1 year ago

Right now, the ProductController also use the CategoryController. Please seperate this data. And modify the producten.blade.php accordanly.

dijkr commented 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();

dijkr commented 1 year ago

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>

dijkr commented 1 year ago

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');