【问题标题】:Query with count and group by eloquent通过 eloquent 进行计数和分组查询
【发布时间】:2018-06-25 00:02:10
【问题描述】:

我想在视图中有一个表格,显示与会议相关的所有注册类型、每种注册类型的价格以及每种注册类型的售出注册数量。

例如,如果 id 为“1”的会议有两种注册类型,其中包含这些列和值:

  • rtype1(名称),0.00$(价格),20(容量)
  • rtype2(名称),5.00$(价格),10(容量)

并且在 rtype1 中注册了 20 个参与者,在 rtype 2 中注册了 5 个参与者我想显示一个表格,如下所示:

Registration Type name               Price           Sold/capacity 
rtype1                               0.00$              20/20
rtype2                               5.00$              5/10

我不明白如何正确地实现这一点。现在我有这个查询来获取每个注册类型和每个注册类型的计数:

$participantsOfEachType = Participant::
 select(DB::raw('registration_type_id, count(registration_type_id)'))
->groupBy('registration_type_id')->get();

此查询返回以下结果。但不考虑具体的会议。您是否知道考虑特定会议 ID 需要什么,以便可以在表格中显示结果,如上表示例中的那样?

Collection {#271 ▼
  #items: array:2 [▼
    0 => Participant {#282 ▼
    ...
      #attributes: array:2 [▼
        "registration_type_id" => 1
        "count(registration_type_id)" => 2
      ]
    ...
    }
    1 => Participant {#279 ▼
    ...
      #attributes: array:2 [▼
        "registration_type_id" => 2
        "count(registration_type_id)" => 2
      ]
      ... 
    }
  ]
}

问题的相关模型:

用户模型:

public function registrations(){
    return $this->hasMany('App\Registration','user_that_did_registration');
}

注册模式:

// user that did the registration
public function customer(){
    return $this->belongsTo(User::class, 'user_that_did_registration', 'id');
}
 public function participants(){
    return $this->hasMany('App\Participant');
}

public function conference(){
    return $this->belongsTo('App\Conference');
}

会议模型:

 public function registrations(){
    return $this->hasMany('App\Registration', 'conference_id');
}

参与者模式:

public function registration(){
    return $this->belongsTo('App\Registration');
}

public function registration_type(){
    return $this->belongsTo('App\RegistrationType');
}

【问题讨论】:

  • 您能否包含您的模型以及它们之间定义的关系,或者为您的表提供表定义和示例数据集
  • 谢谢我用模型更新问题。

标签: laravel eloquent


【解决方案1】:

试试
select conference_id, group_concat(concat_ws(':', registration_type_id, cnt)) from (select conference_id, registration_type_id, count(*) as cnt from t group by conference_id, registration_type_id ) tm group by conference_id order by conference_id;

【讨论】:

  • 谢谢,这个问题是关于 Eloquent 的,但是用那个查询它会显示一个 Table doesn't exist 错误。
  • 你只需要用你的表名替换
【解决方案2】:

我假设您的 RegistrationType 模型具有以下映射

Class RegistrationType Extends Model{

    public function participants(){
        $this->hasMany('App\Participants','registration_type_id')
    }

}

那么对于您的预期输出,您可以获得数据为

$conference_id = 1;

$registrationTypes = RegistrationType::withCount('participants')
                                    ->whereHas('participants.registration.conference',function($query) use ($conference_id){
                                        $query->where('id','=', $conference_id)
                                    });
                                    ->get();

当您遍历结果时,$registrationTypes 中的每一行都会有一个属性 participants_count,它会为您提供该注册类型的参与者数量。

See Counting Related Models & Querying Relationship Existence sections

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2017-06-13
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    相关资源
    最近更新 更多