<i id='obW3E'><tr id='obW3E'><dt id='obW3E'><q id='obW3E'><span id='obW3E'><b id='obW3E'><form id='obW3E'><ins id='obW3E'></ins><ul id='obW3E'></ul><sub id='obW3E'></sub></form><legend id='obW3E'></legend><bdo id='obW3E'><pre id='obW3E'><center id='obW3E'></center></pre></bdo></b><th id='obW3E'></th></span></q></dt></tr></i><div id='obW3E'><tfoot id='obW3E'></tfoot><dl id='obW3E'><fieldset id='obW3E'></fieldset></dl></div>

  • <tfoot id='obW3E'></tfoot>

    <small id='obW3E'></small><noframes id='obW3E'>

  • <legend id='obW3E'><style id='obW3E'><dir id='obW3E'><q id='obW3E'></q></dir></style></legend>

      • <bdo id='obW3E'></bdo><ul id='obW3E'></ul>

        Laravel:如何获得嵌套 hasMany 关系的平均值 (hasMan

        时间:2023-09-23
        <tfoot id='ZCrgB'></tfoot><legend id='ZCrgB'><style id='ZCrgB'><dir id='ZCrgB'><q id='ZCrgB'></q></dir></style></legend>
      1. <i id='ZCrgB'><tr id='ZCrgB'><dt id='ZCrgB'><q id='ZCrgB'><span id='ZCrgB'><b id='ZCrgB'><form id='ZCrgB'><ins id='ZCrgB'></ins><ul id='ZCrgB'></ul><sub id='ZCrgB'></sub></form><legend id='ZCrgB'></legend><bdo id='ZCrgB'><pre id='ZCrgB'><center id='ZCrgB'></center></pre></bdo></b><th id='ZCrgB'></th></span></q></dt></tr></i><div id='ZCrgB'><tfoot id='ZCrgB'></tfoot><dl id='ZCrgB'><fieldset id='ZCrgB'></fieldset></dl></div>

          • <small id='ZCrgB'></small><noframes id='ZCrgB'>

              • <bdo id='ZCrgB'></bdo><ul id='ZCrgB'></ul>

                    <tbody id='ZCrgB'></tbody>

                  本文介绍了Laravel:如何获得嵌套 hasMany 关系的平均值 (hasManyThrough)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有三张桌子:

                  products:   id|name|description|slug|category_id|...
                  reviews:    id|product_id|review_text|name|email|...
                  review_rows id|review_id|criteria|rating
                  

                  review 表存储了评论文本、评论作者,并有一个外部 product_id 键.review_rows 表存储不同标准的评分,例如:

                  the review table stores the review text, writer of the review and has a foreign product_id key. The review_rows table stores the ratings for different criteria like:

                  ----------------------------------------
                  | id |  criteria  | rating | review_id |
                  ----------------------------------------
                  |  1 |  price     | 9      | 12        |
                  ----------------------------------------
                  |  2 |  service   | 8      | 12        |
                  ----------------------------------------
                  |  3 |  price     | 6      | 54        |
                  ----------------------------------------
                  |  4 |  service   | 10     | 54        |
                  ----------------------------------------
                  

                  评论行通过 review_id 外键链接到评论表.我已经像这样设置了我的模型关系:

                  review rows are linked to the review table with the review_id foreign key. I've set up my model relationships like this:

                  Product   -> hasMany   -> Review
                  Review    -> belongsTo -> Product
                  Review    -> hasMany   -> ReviewRow
                  ReviewRow -> belongsTo -> Review
                  

                  现在我想在我的类别和产品页面上显示产品的平均评分.我怎样才能做到这一点?

                  Now I would like to display the average rating for a product on my category and product pages. How can I achieve this?

                  我需要对每条评论的所有评论行求和并求平均值,然后对每条评论的所有评论行求和并求平均值,最终得出该产品的总体评分.这是否可以通过 Eloquent 实现,还是我需要不同的解决方案或不同的数据库设计/结构?

                  I need to sum and average all the reviewRows per review and then sum and average all of those for each review to end up with the overall rating for that product. Is this possible via Eloquent or do I need a different solution or a different database design/structure?

                  提前致谢!

                  推荐答案

                  你需要这样的东西 http://softonsofa.com/tweaking-eloquent-relations-how-to-get-hasmany-relation-count-efficiently/ 仅略微调整为满足您的需求:

                  You need something like this http://softonsofa.com/tweaking-eloquent-relations-how-to-get-hasmany-relation-count-efficiently/ only slightly adjusted to match your needs:

                  public function reviewRows()
                  {
                      return $this->hasManyThrough('ReviewRow', 'Review');
                  }
                  
                  public function avgRating()
                  {
                      return $this->reviewRows()
                        ->selectRaw('avg(rating) as aggregate, product_id')
                        ->groupBy('product_id');
                  }
                  
                  public function getAvgRatingAttribute()
                  {
                      if ( ! array_key_exists('avgRating', $this->relations)) {
                         $this->load('avgRating');
                      }
                  
                      $relation = $this->getRelation('avgRating')->first();
                  
                      return ($relation) ? $relation->aggregate : null;
                  }
                  

                  就这么简单:

                  // eager loading
                  $products = Product::with('avgRating')->get();
                  $products->first()->avgRating; // '82.200' | null
                  
                  // lazy loading via dynamic property
                  $product = Product::first()
                  $product->avgRating; // '82.200' | null
                  

                  这篇关于Laravel:如何获得嵌套 hasMany 关系的平均值 (hasManyThrough)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Eloquent 的 Laravel 块方法 下一篇:Laravel 变形关系

                  相关文章

                  最新文章

                • <i id='bYW4E'><tr id='bYW4E'><dt id='bYW4E'><q id='bYW4E'><span id='bYW4E'><b id='bYW4E'><form id='bYW4E'><ins id='bYW4E'></ins><ul id='bYW4E'></ul><sub id='bYW4E'></sub></form><legend id='bYW4E'></legend><bdo id='bYW4E'><pre id='bYW4E'><center id='bYW4E'></center></pre></bdo></b><th id='bYW4E'></th></span></q></dt></tr></i><div id='bYW4E'><tfoot id='bYW4E'></tfoot><dl id='bYW4E'><fieldset id='bYW4E'></fieldset></dl></div>

                  1. <small id='bYW4E'></small><noframes id='bYW4E'>

                  2. <legend id='bYW4E'><style id='bYW4E'><dir id='bYW4E'><q id='bYW4E'></q></dir></style></legend>

                    <tfoot id='bYW4E'></tfoot>

                      • <bdo id='bYW4E'></bdo><ul id='bYW4E'></ul>