Home > Software design >  Get data for Last 7 days from database for each day in laravel
Get data for Last 7 days from database for each day in laravel

Time:12-10

Good Evening.

I am trying to get the data from database for Only for last 7 days and show the same in table.

I tried the below code, but its giving me data for 30 days. I am new to coding and a self learner,

Your help will be helpful...

controller code

$sevendays = Carbon::now()->subDays(7);

$dairymilksaleweek =    customermilksale::selectraw('(saledate) as "startdate", (SUM(buffalomilk)) as "totalbmilk", (SUM(a2milk)) as "totala2milk", (SUM(jerseymilk)) as "totaljmilk", (SUM(totalmilk)) as "totalmilk"')
                                                ->whereDate('saledate', '>=', now()->subDays(7)->startOfDay())
                                                ->groupBy('saledate')
                                                ->orderBy('saledate')
                                                ->get();

Thanks in advance...

CodePudding user response:

As per your query you wanted to get data for last seven days and you are using the whereDate() method of laravel eloquent which needs dates in specified format i.e format('d/m/Y') don't know what's the format for your saledate column but you need to convert second parameter of the function to one of the below. 1- today()->subDays(7) 2- now()->subDays(7)->startOfDay()->format('d/m/Y')

Hope this will help you out.

  • Related