Laravel Eloquent GroupBy声明

时间:2017-08-01 16:12:28

标签: laravel laravel-5 eloquent

我有这张桌子,我正试图通过持续时间总和通过Laravel获得多个groupBydistinct。基本上,我需要在表中找到从控制器传递的多个id,然后如果窗口,方法和类型相同,则将持续时间值相加。

这是表结构

+-----+---------+--------+----------+------+
| ids | windows | method | duration | type |
+-----+---------+--------+----------+------+
|  1  |    2    |    3   |     5    |   2  |
+-----+---------+--------+----------+------+
|  2  |    2    |    3   |     5    |   2  |
+-----+---------+--------+----------+------+
|  3  |    2    |    3   |     5    |   2  |
+-----+---------+--------+----------+------+

以下是我尝试使用的声明:

$tickets_g = \App\Models\Ticked::whereIn('id', $tickets)
    ->select('categoria', 'tipologia', 'priority', 'durata')
    ->distinct()
    ->GroupBy('categoria')
    ->GroupBy('tipologia')
    ->GroupBy('priority')
    ->sum('durata')
    ->get();

有人可以帮我解决一下吗?

1 个答案:

答案 0 :(得分:0)

修正了自己,如果有人遇到同样的问题,这是正确的查询:

  $tickets_g = \App\Models\Ticked::select('support_ticked.*', DB::raw("SUM(support_ticked.durata) as durata_totale"))
->groupBy('support_ticked.tipologia','support_ticked.priority', 'support_ticked.categoria')
->get();