Laravel using Sum and Groupby

前端 未结 2 1380
庸人自扰
庸人自扰 2020-12-12 01:49

I would like to fetch sum of quantity in each month, so that I can display on bar chart quantities against month

This is what I thought but didn\'t workout

2条回答
  •  萌比男神i
    2020-12-12 02:26

    try this query , you will get month wise count :

    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();
    

提交回复
热议问题