'For Chart - Trying to Get data for last 30 days and add "0" if data is not available in particular date
I am trying to draw chart for Last 30 days from Today showing total milk quantity. I get last 30 days using - [0 => "2022-02-07" ----- 30 => "2022-03-09"]
$olddate = Carbon::today()->subDays(30)->toDateString();
$todaydate = Carbon::today()->toDateString();
Also get the Data for Database as below [ date" => "2022-03-01" "totalmilk" => "24.10" "daykey" => "01"................."date" => "2022-02-16" "totalmilk" => "22.90" "daykey" => "16"]
$rahul = Buffalomilkrecord::select(DB::raw('date'), DB::raw('sum(totalmilk) as totalmilk, DATE_FORMAT(date,"%d") as "daykey"'))
->whereBetween('date', [$olddate, $todaydate] )
->groupBy(DB::raw('date'))
->orderBy('date', 'desc')
->get();
BY Below code, trying to put "0" if data is not Available
$ddtest = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
foreach($rahul as $order)
{
$ddtest[$order->daykey-1] = $order->date;
}
The code is working fine except the start date he is taking the database date which is (2022-03-01) where it should start with "2022-02-07".
I think i am making mistake while getting "daykey". It is assigning daykey "1" to first data available in database which start from date ""2022-03-01" (as No data available from 07-02-2022 to 28-02-2022 in the database and it start from 1 March 2022.) where as it should start from "$olddate" that is ""2022-02-07"..
where did i am making the mistake.....Thanks in Advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
