从枢轴表在laravel雄辩 [英] Getting count from pivot table in laravel eloquent

查看:154
本文介绍了从枢轴表在laravel雄辩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对订单和产品有许多关系。

 <?php 
class Order extend Eloquent {

public function user()
{
return $ this-> belongsTo('User');
}

public function products()
{
return $ this-> belongsToMany('Product');
}
}
?>


<?php
类产品扩展Eloquent {

公共函数orders()
{
return $ this - > belongsToMany('Order');
}

}
?>

需要获取每个产品的订购次数。在mysql中,此任务可以通过使用以下查询

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

上述查询的结果如下: -

  id说明计数(products.id)
1鞋3
2包2
3太阳镜2
4衬衫2

如何使用laravel eloquent实现这个任务(不使用查询生成器)????如何获取次数

注意 Eloquent 每个产品都使用laravel eloquent命令

解决方案使用 Query\Builder ,因此在Laravel中没有这样的东西,像'query eloquent without using query builder'。



这是你需要的:

  //计数的附加助手关系
public函数ordersCount()
{
return $ this-> belongsToMany('Order')
- > selectRaw('count(orders.id)as aggregate')
- > groupBy('pivot_product_id');
}

//访问器更容易获取计数
public function getOrdersCountAttribute()
{
if(!array_key_exists('ordersCount',$ this - > relation))$ this-> load('ordersCount');

$ related = $ this-> getRelation('ordersCount') - > first();

return($ related)? $ related-> aggregate:0;
}

这将让您充分利用急速加载:

  $ products = Product :: with('ordersCount') - > get() 

//然后对于每个产品,你可以这样调用它
$ products-> first() - > ordersCount; //感谢访问者

详细了解 Eloquent accessors& mutators



和关于动态属性






当然,您可以使用简单的连接来获得完全相同的查询在您的示例中。


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

 }
?>

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

Result of the above query is as follows:-

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

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

解决方案

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

And this is what you need:

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

Read more about Eloquent accessors & mutators,

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雄辩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆