久久久久久久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>
                          • 主站蜘蛛池模板: 91av视频| 福利网站在线观看 | 中文字幕一区在线观看视频 | 亚洲激情网站 | 日本一二三区在线观看 | 亚洲视频在线一区 | 99久久精品国产一区二区三区 | 免费在线成人 | 国产成人久久久 | 国产国语精品 | 欧美亚洲视频 | 一区二区三区在线免费观看视频 | 久久久性色精品国产免费观看 | 玖玖操| 亚洲视频一区在线播放 | 日韩www | 亚洲一区二区中文字幕在线观看 | 亚洲成人观看 | 老司机午夜性大片 | 中文字幕第十页 | 久久久一二三区 | 免费看黄视频网站 | av在线成人| 91综合网| 国产乱码精品1区2区3区 | 国产精品美女久久久久aⅴ国产馆 | 国产情侣久久 | 手机看片1| 午夜视频在线观看网站 | 69堂永久69tangcom | 久久成人精品视频 | 久草在线 | 国产在线观看免费 | 久久久精品网站 | 亚洲国产一区二区在线 | 天天看逼 | 操操日| 久久夜视频| 黄片毛片免费看 | 精品国产一区二区在线 | 国产成人高清 |