<legend id='Zb7wC'><style id='Zb7wC'><dir id='Zb7wC'><q id='Zb7wC'></q></dir></style></legend>

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

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

        <tfoot id='Zb7wC'></tfoot>

        从 laravel eloquent 中的数据透视表中获取计数

        Getting count from pivot table in laravel eloquent(从 laravel eloquent 中的数据透视表中获取计数)
        <i id='xZUVj'><tr id='xZUVj'><dt id='xZUVj'><q id='xZUVj'><span id='xZUVj'><b id='xZUVj'><form id='xZUVj'><ins id='xZUVj'></ins><ul id='xZUVj'></ul><sub id='xZUVj'></sub></form><legend id='xZUVj'></legend><bdo id='xZUVj'><pre id='xZUVj'><center id='xZUVj'></center></pre></bdo></b><th id='xZUVj'></th></span></q></dt></tr></i><div id='xZUVj'><tfoot id='xZUVj'></tfoot><dl id='xZUVj'><fieldset id='xZUVj'></fieldset></dl></div>

              <bdo id='xZUVj'></bdo><ul id='xZUVj'></ul>
              <legend id='xZUVj'><style id='xZUVj'><dir id='xZUVj'><q id='xZUVj'></q></dir></style></legend>
                <tbody id='xZUVj'></tbody>
              <tfoot id='xZUVj'></tfoot>

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

                  本文介绍了从 laravel eloquent 中的数据透视表中获取计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的订单和产品是多对多关系.

                  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');
                      }
                  
                   }
                  ?>
                  

                  需要获取每个产品的订购次数.在mysql中,这个任务可以通过以下查询来完成

                  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
                  

                  以上查询结果如下:-

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

                  如何使用 laravel eloquent 完成此任务(不使用查询构建器)????我如何使用 laravel eloquent 获取每个产品的订购次数??

                  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 中没有这样的东西,例如不使用查询生成器的查询雄辩".

                  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;
                  }
                  

                  这将让您利用预先加载:

                  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
                  

                  阅读更多关于 Eloquent 访问器和突变体,

                  关于动态属性,上面的访问器模仿的行为.

                  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.

                  这篇关于从 laravel eloquent 中的数据透视表中获取计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  <tfoot id='NXerR'></tfoot>
                  • <small id='NXerR'></small><noframes id='NXerR'>

                      <tbody id='NXerR'></tbody>
                    <legend id='NXerR'><style id='NXerR'><dir id='NXerR'><q id='NXerR'></q></dir></style></legend>
                    • <bdo id='NXerR'></bdo><ul id='NXerR'></ul>
                        1. <i id='NXerR'><tr id='NXerR'><dt id='NXerR'><q id='NXerR'><span id='NXerR'><b id='NXerR'><form id='NXerR'><ins id='NXerR'></ins><ul id='NXerR'></ul><sub id='NXerR'></sub></form><legend id='NXerR'></legend><bdo id='NXerR'><pre id='NXerR'><center id='NXerR'></center></pre></bdo></b><th id='NXerR'></th></span></q></dt></tr></i><div id='NXerR'><tfoot id='NXerR'></tfoot><dl id='NXerR'><fieldset id='NXerR'></fieldset></dl></div>