Laravel使用Sum和Groupby

时间:2018-08-27 14:18:44

标签: php laravel eloquent laravel-5.6 laravel-collection

我想获取每个月的数量总和,以便可以在条形图上显示相对于月份的数量

这就是我的想法,但是没有锻炼

GeneralizedTime

我的表结构

newtype DescribeViaTypeable a = DVT a
newtype DescribeViaShow     a = DVS a

instance Show     a => Described (DescribeViaShow     a) where describe (DVS x) = show x
instance Typeable a => Described (DescribeViaTypeable a) where describe (DVT x) = show (typeOf x)

2 个答案:

答案 0 :(得分:3)

该收集组不是雄辩的分组依据

如果您想雄辩地做到这一点,那就得

image1 = load("background1.png")
x = image1[1]
image1[1] = RGB(0,x.g,x.b)

如果要使用集合groupBy方法执行此操作,则应首先执行get,然后执行groupBy。

尽管如此,尽管我不确定您要在回调中执行的操作。

 $data1 = Borrow::selectRaw('SUM(quantity) as qt, MONTH(created_at) as borrowMonth')
      ->groupBy('borrowMonth')->get();

答案 1 :(得分:0)

尝试此查询,您将获得按月统计:

use DB;


 $month_wise_count=DB::table("borrows")
        ->select(DB::raw('CONCAT(MONTHNAME(created_at), "-",  YEAR(created_at)) AS month_year'),
                DB::raw("MONTH(created_at) as month , YEAR(created_at) as year"),
                DB::raw("(COUNT(*)) as total_records"),
                DB::row("(SUM('quantity') as total_value"))
        ->orderBy(DB::raw("MONTH(created_at),YEAR(created_at)"))
        ->groupBy(DB::raw("MONTH(created_at),YEAR(created_at)"))
        ->get();
相关问题