Home > Back-end >  Datatable order by Month and Year
Datatable order by Month and Year

Time:03-06

Can i set datatable order by Month and Year ( where i get the data as "October 2021",...."February 2022" ). Refer the attached image for details

This is my code in controller where i get data .

 $fiveexpdata = Dairyexpense::select(DB::raw('DATE_FORMAT(expensesdate, "%M %Y") as "month_name",SUM(amount) as "amount"'))
          ->whereBetween('expensesdate', [Carbon::now()->subMonth(6),Carbon::now()->endOfMonth()])
          ->groupBy('month_name')
          ->get()
          ->toArray();

After Sending the same in datatable, it shows data in Oct 2021 Nov 2021 Dec 2021 Jan 2022 Feb 2022 March 2022

Where as i required the same in March 2022 Feb 2022 Jan 2022 Dec 2021 Nov 2021 Oct 2021

enter image description here

CodePudding user response:

Just like said in this :

Group by month and order the dates on Laravel Eloquent

Do you try do add that ?

  ->DB::raw('max(expensesdate) as expensesdate')
  ->orderBy('expensesdate', 'desc')
  • Related