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

        • <bdo id='B3j22'></bdo><ul id='B3j22'></ul>
        <tfoot id='B3j22'></tfoot><legend id='B3j22'><style id='B3j22'><dir id='B3j22'><q id='B3j22'></q></dir></style></legend>

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

        <i id='B3j22'><tr id='B3j22'><dt id='B3j22'><q id='B3j22'><span id='B3j22'><b id='B3j22'><form id='B3j22'><ins id='B3j22'></ins><ul id='B3j22'></ul><sub id='B3j22'></sub></form><legend id='B3j22'></legend><bdo id='B3j22'><pre id='B3j22'><center id='B3j22'></center></pre></bdo></b><th id='B3j22'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='B3j22'><tfoot id='B3j22'></tfoot><dl id='B3j22'><fieldset id='B3j22'></fieldset></dl></div>
      1. 在laravel eloquent中從數(shù)據(jù)透視表中獲取計數(shù)

        Getting count from pivot table in laravel eloquent(在laravel eloquent中從數(shù)據(jù)透視表中獲取計數(shù))

            • <bdo id='S3s9o'></bdo><ul id='S3s9o'></ul>
              <tfoot id='S3s9o'></tfoot>

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

                <i id='S3s9o'><tr id='S3s9o'><dt id='S3s9o'><q id='S3s9o'><span id='S3s9o'><b id='S3s9o'><form id='S3s9o'><ins id='S3s9o'></ins><ul id='S3s9o'></ul><sub id='S3s9o'></sub></form><legend id='S3s9o'></legend><bdo id='S3s9o'><pre id='S3s9o'><center id='S3s9o'></center></pre></bdo></b><th id='S3s9o'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='S3s9o'><tfoot id='S3s9o'></tfoot><dl id='S3s9o'><fieldset id='S3s9o'></fieldset></dl></div>
                • <legend id='S3s9o'><style id='S3s9o'><dir id='S3s9o'><q id='S3s9o'></q></dir></style></legend>
                    <tbody id='S3s9o'></tbody>
                  本文介紹了在laravel eloquent中從數(shù)據(jù)透視表中獲取計數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有訂單和產(chǎn)品的多對多關(guān)系.

                  I have a many to many relationship for orders and products.

                  <?php
                  class Order extends Eloquent {
                  
                      public function user()
                      {
                          return $this->belongsTo('User');
                      }
                  
                      public function products()
                      {
                          return $this->belongsToMany('Product');
                      }
                   }
                   ?>
                  
                  
                  <?php
                  class Product extends Eloquent {
                  
                      public function orders()
                      {
                          return $this->belongsToMany('Order');
                      }
                  
                   }
                  ?>
                  

                  需要獲取每個產(chǎn)品的訂購次數(shù).在mysql中,這個任務(wù)可以通過使用下面的查詢來實現(xiàn)

                  Need to fetch the number of times each product is ordered.In mysql,this task can be achieved by using the following query

                  SELECT products.id, products.description, count( products.id )
                  FROM products
                  INNER JOIN order_product ON products.id = order_product.product_id
                  INNER JOIN orders ON orders.id = order_product.order_id
                  GROUP BY product_id
                  LIMIT 0 , 30
                  

                  以上查詢結(jié)果如下:-

                  id  description   count(products.id)    
                   1     Shoes          3
                   2     Bag            2
                   3     Sun glasses    2
                   4     Shirt          2
                  

                  如何使用 laravel eloquent 完成這項任務(wù)(不使用查詢構(gòu)建器)????如何使用 laravel eloquent 獲取每個產(chǎn)品的訂購次數(shù)??

                  How this task can be achieved using laravel eloquent (without using query builder)????How can i fetch the number of times each product is ordered using laravel eloquent??

                  推薦答案

                  注意 Eloquent 在底層使用 QueryBuilder,所以 Laravel 中沒有這樣的東西,就像'查詢雄辯而不使用查詢構(gòu)建器'.

                  Mind that Eloquent uses QueryBuilder under the hood, so there is no such thing in Laravel, like 'query eloquent without using query builder'.

                  這就是你所需要的:

                  // additional helper relation for the count
                  public function ordersCount()
                  {
                      return $this->belongsToMany('Order')
                          ->selectRaw('count(orders.id) as aggregate')
                          ->groupBy('pivot_product_id');
                  }
                  
                  // accessor for easier fetching the count
                  public function getOrdersCountAttribute()
                  {
                      if ( ! array_key_exists('ordersCount', $this->relations)) $this->load('ordersCount');
                  
                      $related = $this->getRelation('ordersCount')->first();
                  
                      return ($related) ? $related->aggregate : 0;
                  }
                  

                  這將讓您利用預(yù)先加載的優(yōu)勢:

                  This will let you take advantage of eager loading:

                  $products = Product::with('ordersCount')->get();
                  
                  // then for each product you can call it like this
                  $products->first()->ordersCount; // thanks to the accessor
                  

                  閱讀更多關(guān)于 Eloquent accessors &變異子,

                  以及動態(tài)屬性,上面的訪問器模仿了這些行為.

                  and about dynamic properties, of which behaviour the above accessor mimics.

                  當然,您可以使用簡單的連接來獲得與示例中完全相同的查詢.

                  Of course you could use simple joins to get exactly the same query like in you example.

                  這篇關(guān)于在laravel eloquent中從數(shù)據(jù)透視表中獲取計數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務(wù)器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數(shù))
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結(jié)果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問被拒絕)
                  <legend id='ooHfR'><style id='ooHfR'><dir id='ooHfR'><q id='ooHfR'></q></dir></style></legend>
                    <bdo id='ooHfR'></bdo><ul id='ooHfR'></ul>

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

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

                          • <i id='ooHfR'><tr id='ooHfR'><dt id='ooHfR'><q id='ooHfR'><span id='ooHfR'><b id='ooHfR'><form id='ooHfR'><ins id='ooHfR'></ins><ul id='ooHfR'></ul><sub id='ooHfR'></sub></form><legend id='ooHfR'></legend><bdo id='ooHfR'><pre id='ooHfR'><center id='ooHfR'></center></pre></bdo></b><th id='ooHfR'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ooHfR'><tfoot id='ooHfR'></tfoot><dl id='ooHfR'><fieldset id='ooHfR'></fieldset></dl></div>
                              <tbody id='ooHfR'></tbody>
                          • 主站蜘蛛池模板: 午夜免费观看体验区 | 污污免费网站 | 久久精品16 | 亚洲一二三区免费 | 天堂av中文 | 黄视频免费观看 | 天天爽天天干 | 日韩高清成人 | 97精品视频在线 | 在线欧美亚洲 | 欧美一级毛片久久99精品蜜桃 | 亚州综合在线 | 欧美日韩一区二区三区在线观看 | 中文字幕11页 | 欧美精品日韩精品国产精品 | 中文字幕在线看人 | 亚洲一区二区国产 | 欧美高清免费 | 国产不卡在线播放 | 成年无码av片在线 | 丁香综合 | 一区二区三区四区国产 | 91免费看片神器 | 一级黄色影片在线观看 | 国产草草视频 | 激情久久久久 | 一级毛毛片 | 蜜桃视频在线观看免费视频网站www | 久久精品国产99国产精品 | 久久大 | 中文字幕精品一区二区三区精品 | 欧美 日韩 国产 成人 | 日韩在线看片 | 欧美精品在欧美一区二区 | 日韩在线一区二区三区 | 91毛片在线观看 | 特黄视频 | 99av成人精品国语自产拍 | 性高朝久久久久久久3小时 av一区二区三区四区 | 黄色成人av| 狠狠综合久久av一区二区小说 |