add monthly rainfalls

This commit is contained in:
Romulus21
2024-03-09 15:33:23 +01:00
parent 7244cd7be4
commit 77412774c8
9 changed files with 100 additions and 130 deletions

View File

@@ -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);
}
}