久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<tfoot id='ZCrgB'></tfoot><legend id='ZCrgB'><style id='ZCrgB'><dir id='ZCrgB'><q id='ZCrgB'></q></dir></style></legend>
  • <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 class="qwawimqqmiuu" id='ZCrgB'><tfoot id='ZCrgB'></tfoot><dl id='ZCrgB'><fieldset id='ZCrgB'></fieldset></dl></div>

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

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

      1. Laravel:如何獲得嵌套 hasMany 關系的平均值 (hasMan

        Laravel: how to get average on nested hasMany relationships (hasManyThrough)(Laravel:如何獲得嵌套 hasMany 關系的平均值 (hasManyThrough))
        <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 class="qwawimqqmiuu" id='obW3E'><tfoot id='obW3E'></tfoot><dl id='obW3E'><fieldset id='obW3E'></fieldset></dl></div>

      2. <tfoot id='obW3E'></tfoot>

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

      3. <legend id='obW3E'><style id='obW3E'><dir id='obW3E'><q id='obW3E'></q></dir></style></legend>

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

                  本文介紹了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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)

                    <tbody id='bYW4E'></tbody>
                  <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 class="qwawimqqmiuu" id='bYW4E'><tfoot id='bYW4E'></tfoot><dl id='bYW4E'><fieldset id='bYW4E'></fieldset></dl></div>

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

                    • <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>
                            主站蜘蛛池模板: 成人超碰在线 | 国产精品观看 | 午夜精品久久久久久久久久久久 | 国产精品久久久久久久久久三级 | 波多野结衣一区二区 | 成人av一区二区三区 | 黄视频在线网站 | av电影一区二区 | 欧美日韩成人影院 | 亚洲精品成人在线 | av黄色片| 成人一级黄色毛片 | 黄色免费av | 久久最新精品 | 色99视频 | 毛片一区二区 | 午夜小电影 | 亚洲视频一区在线观看 | 欧美日韩专区 | 国产性生活一级片 | 99免费| 国产精品夜夜春夜夜爽久久电影 | 二区视频 | 91在线播 | 亚洲成人av在线 | 国产乱码久久久久久 | 久久亚洲一区二区 | 国产精品久久久久久久午夜 | www.操.com | 午夜寂寞影院列表 | 亚洲二区视频 | 日韩国产一区二区 | 色婷婷av久久久久久久 | 成人av高清在线观看 | 一区二区三区免费 | 在线视频日韩 | 欧美视频在线播放 | www.亚洲一区 | 国产免费一区二区三区 | 在线看一区二区三区 | 国产精品久久久久久久久久尿 |