简体   繁体   English

Laravel有很多分组

[英]Laravel has many and grouping

I can't seem to get the syntax right for this query. 我似乎无法正确获取此查询的语法。

Basically, I have a product that is listed, where an offer is made by a user: 基本上,我列出了一种产品,该产品由用户提供:

So in Offer.php 所以在Offer.php

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

And in User.php 并在User.php

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

Then in Product.php 然后在Product.php

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

I need to group offers by the user that has made them, and then group that again by the category of the product the offer is against. 我需要按提供报价的用户对报价进行分组,然后再次按报价所针对的产品类别对报价进行分组。

Does anyone have ideas where I can start to group these correctly? 有没有人有想法可以将它们正确地分组?

Try with Query Builder . 尝试使用Query Builder Like this ( I don't know your tables`s structure): 这样(我不知道你表的结构):

 $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