Query for retrieving data records for a specific date range in Laravel 4

前端 未结 2 1509
北海茫月
北海茫月 2021-02-11 04:54

I have a table having record_date and corresponding amount field.I was trying to retrieve total amount grouping by the months of the date .

I a

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-11 05:27

    You need a raw query and you may try this (Not tested but should work):

    $data = DB::table('table_name')
      ->select(DB::raw('MONTH(record_date) as m, YEAR(record_date) as y, SUM(amount) as t'))
      ->whereRaw('record_date > DATE_SUB(now(), INTERVAL 6 MONTH)')
      ->groupBy(DB::raw('YEAR(record_date), MONTH(created_at)'))
      ->get();
    

提交回复
热议问题