Home > Back-end >  How get get data "orderBy" months but start with Current month and so on
How get get data "orderBy" months but start with Current month and so on

Time:03-04

Trying to get data by months and for first part, its working perfectly and now stuck at 2 points.

  1. Required Month Name with Year ( Say "March 2022" )
  2. Required the same in OrderBy where it should start with current month (March 2022) and then further (april 2022,May 2022,June 2022 .... Jan 2022, Feb 2022)

My Controller

         $getdatapermonth    =   Dairyincome::select(
            DB::raw('(MONTHNAME(date)) as "month_name",SUM(cmilkquantity) as "cmilkquantity", SUM(cmilkamount) as "cmilkamount",SUM(bmilkquantity) as "bmilkquantity", SUM(bmilkamount) as "bmilkamount", SUM(totalamount) as "totalamount"')
            )
            ->where('customerid',$customerid)   
            ->whereYear('date', date('Y'))
            ->groupBy('month_name')
            ->get()->toArray();

Array i am getting

    array:3 [▼
0 => array:6 [▼
"month_name" => "January"
"cmilkquantity" => "0.00"
"cmilkamount" => "0.00"
"bmilkquantity" => "10.00"
"bmilkamount" => "450.00"
"totalamount" => "450.00"
 ]
 1 => array:6 [▼
"month_name" => "February"
"cmilkquantity" => "2.00"
"cmilkamount" => "70.00"
"bmilkquantity" => "0.00"
"bmilkamount" => "0.00"
"totalamount" => "70.00"
 ]
 2 => array:6 [▼
"month_name" => "March"
"cmilkquantity" => "2.00"
"cmilkamount" => "70.00"
"bmilkquantity" => "2.00"
"bmilkamount" => "70.00"
"totalamount" => "140.00"

] ]

Thanks in Advance.

CodePudding user response:

Just change:

DB::raw('(MONTHNAME(date)) as "month_name"')

to:

DB::raw('DATE_FORMAT(date, "%M %Y") as "month_name"')

It will output month name in "Month YYYY" format.

  • Related