I have an eCommerce site with a Product model and a ProductCategory model. I currently show products on the page from the Product model but would like to be able to get a list of all the categories with products in the current model and how many products are in each category. I can get the overall count but can't figure out how to get the list of categories being shown and how many results per category have been returned.
Product Model
ProductCategory Model
Currently, I access the results in the blade using...
@foreach($products->chunk(3) as $row)
<div class="item-row">
@foreach($row as $product)
<div class="item item-thumbnail" style="height: 250px;">
<a href="product_detail.html" class="item-image">
@if(empty($product->Images{0}->original_image))
<img style="width: 100px; height: 100px;"
src="https://example.com/100x100/d3d3d3/fff.gif&text=No+Image"
alt="" />
@else
<img style="width: 100px; height: 100px;"
src="https://cdn.example.com.au/products/{{$product->id}}/{{$product->Images{0}->original_image}}"
alt="" />
@endif
</a>
<div class="item-info">
<h4 class="item-title">
<a href="/store/{{$category->alias}}/{{$product->alias}}">
{{$product->product_name}}
</a>
</h4>
<p class="item-desc"> </p>
@if(empty($product->special_price))
<div class="item-price">
${{$product->normal_price}}
</div>
@else
<div class="item-price">
${{$product->special_price}}
</div>
<div class="item-discount-price">
${{$product->normal_price}}
</div>
@endif
</div>
</div>
@endforeach
</div>
@endforeach
And would like to be able to generate a list of all the categories with products displayed as...
@foreach($products->categories as $category)
<li>
{{$category->category_name}} ({{$category->count}})
</li>
@endforeach
All from within the same model.
If it helps clarify I don't want the model to change drastically in that I still want to be able to access the products in the model from the blade template as is currently done but would like to also be able to pull a list of categories such as the below example...
| Product_ID | Product_Name | Category_ID |
| ---------- | ------------ | ----------- |
| 1 | Product 1 | 1 |
| 2 | Product 2 | 1 |
| 3 | Product 3 | 2 |
| Category ID | Category Name |
| ----------- | ------------- |
| 1 | Category 1 |
| 2 | Category 2 |
And wind up with the following table on my page to show the product categories being shown in the results...
| Category Name | # Products |
| ------------- | ---------- |
| Category 1 | 2 |
| Category 2 | 1 |
It would be super helpful if you provided the code where you query the products and categories.
But here's essentially what you need to do. You need to make use of ->withCount(..)
:
$products = Product::with([
'categories' => function ($query) {
// Adds count of category-related products
$query->withCount('products as count');
},
])->get();
And then this would work in your view:
@foreach($products as $product)
@foreach($product->categories as $category)
<li>{{$category->category_name}} ({{$category->count}})</li>
@endforeach
@endforeach