• <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 id='S3s9o'><tfoot id='S3s9o'></tfoot><dl id='S3s9o'><fieldset id='S3s9o'></fieldset></dl></div>
    1. <legend id='S3s9o'><style id='S3s9o'><dir id='S3s9o'><q id='S3s9o'></q></dir></style></legend>

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

      时间:2023-09-23

            <tbody id='B3j22'></tbody>
            <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 id='B3j22'><tfoot id='B3j22'></tfoot><dl id='B3j22'><fieldset id='B3j22'></fieldset></dl></div>
              • 本文介绍了在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 accessors &变异子,

                以及动态属性,上面的访问器模仿了这些行为.

                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中从数据透视表中获取计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 Laravel 中对自定义查询的结果进行分块 下一篇:laravel eloquent 嵌套评论和回复

                相关文章

                最新文章

                <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'>

                  1. <tfoot id='ooHfR'></tfoot>

                  2. <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 id='ooHfR'><tfoot id='ooHfR'></tfoot><dl id='ooHfR'><fieldset id='ooHfR'></fieldset></dl></div>