行和总和使用雄辩 [英] Sum of rows and groupby using eloquent

查看:42
本文介绍了行和总和使用雄辩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下列:

category, rating, num_users, date_voted /*number of users*/

如何使用雄辩的方式将给定日期范围内的用户数之和归类为类别

How do I get the rating grouped by category as the sum of the number of users over a given range of dates using eloquent

Category::groupBy('num_users') //returns the categories

主表数据示例

--------------------------------
category| rating| num_users| date_voted
---------------------------------------
Action  |5      |2         | 12-12-2015
Comedy  |2      |2         | 12-12-2015
Fantasy |3      |2         | 12-12-2015
Action  |4      |2         | 14-12-2015
Comedy  |5      |2         | 14-12-2015

我希望html表的形式为类别vs评分(介于1到5之间),例如

I want the html table to be in the form of category vs ratings(between 1 and 5) eg

-----------------------------------------------------
Category|Rating 1|Rating 2|Rating 3|Rating 4|Rating 5|
------------------------------------------------------
Action  |2       |3       |5       |5       |9       |  
Comedy  |9       |8       |6       |9       |14      | 
Fantasy |1       |2       |3       |6       |7       | 

推荐答案

也许会帮助您

Category::select(DB::raw('sum(users) as total_users'),'category')->groupBy('category')->get();

这篇关于行和总和使用雄辩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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