add monthly rainfalls
This commit is contained in:
@@ -110,4 +110,27 @@ class RainfallController extends Controller
|
||||
|
||||
return response()->json(array_values($results));
|
||||
}
|
||||
|
||||
public function lastMonths(Request $request)
|
||||
{
|
||||
$result = [];
|
||||
for ($i = 12; $i >= 0; $i--) {
|
||||
$date = now()->subMonths($i);
|
||||
$firstOfMonth = now()->subMonths($i)->firstOfMonth();
|
||||
$lastOfMonth = now()->subMonths($i)->lastOfMonth();
|
||||
$rainfalls = $request->user()
|
||||
->rainfalls()
|
||||
->whereBetween('date', [$firstOfMonth, $lastOfMonth])
|
||||
->sum('value');
|
||||
|
||||
$result[] = [
|
||||
'year' => $date->year,
|
||||
'month' => $date->month,
|
||||
'label' => $date->monthName.' '.$date->year,
|
||||
'values' => (int) $rainfalls,
|
||||
];
|
||||
}
|
||||
|
||||
return response()->json($result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user