douxing1850 2018-12-14 22:17
浏览 98

Laravel模型按关系计数

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

  • Product_ID
  • Product_Name
  • Product_Description
  • Category_ID (Many-To-1: ProductCategory.Category_ID

ProductCategory Model

  • Category_ID
  • Category_Name

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">&nbsp;</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.

Additional

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          |
  • 写回答

1条回答 默认 最新

  • douxi7219 2018-12-15 01:09
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用