繁体   English   中英

Laravel有很多分组

[英]Laravel has many and grouping

我似乎无法正确获取此查询的语法。

基本上,我列出了一种产品,该产品由用户提供:

所以在Offer.php

public function user()
{
    return $this->belongsTo('App\User', 'buyer_id');
}
public function product(){
    return $this->belongsTo('App\Product');
}

并在User.php

public function offers()
{
    return $this->hasMany('App\Offer', 'buyer_id');
}

然后在Product.php

public function offer()
{
    return $this->hasMany('App\Offer');
}

我需要按提供报价的用户对报价进行分组,然后再次按报价所针对的产品类别对报价进行分组。

有没有人有想法可以将它们正确地分组?

尝试使用Query Builder 这样(我不知道你表的结构):

 $offers = DB::table('offers')
             ->joinLeft('users','users.id','=','offers.buyer_id')
             ->joinLeft('products','products.id','=','offers.product_id')
             ->groupBY('users.id','products.category_id')
             ->get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM