improve months values
This commit is contained in:
@@ -113,9 +113,17 @@ class RainfallController extends Controller
|
||||
|
||||
public function lastMonths(Request $request)
|
||||
{
|
||||
$lang = array_values(array_filter($request->getLanguages(), fn ($v) => str_contains($v, '_')))[0] ?? 'en_US';
|
||||
setlocale(LC_TIME, $lang);
|
||||
Carbon::setLocale(explode('_', $lang)[0]);
|
||||
|
||||
$firstOfLastYear = now()->subYear()->firstOfYear();
|
||||
$diff = now()->diffInMonths($firstOfLastYear);
|
||||
|
||||
$result = [];
|
||||
for ($i = 12; $i >= 0; $i--) {
|
||||
for ($i = $diff; $i >= 0; $i--) {
|
||||
$date = now()->subMonths($i);
|
||||
$month = $date->month;
|
||||
$firstOfMonth = now()->subMonths($i)->firstOfMonth();
|
||||
$lastOfMonth = now()->subMonths($i)->lastOfMonth();
|
||||
$rainfalls = $request->user()
|
||||
@@ -123,7 +131,11 @@ class RainfallController extends Controller
|
||||
->whereBetween('date', [$firstOfMonth, $lastOfMonth])
|
||||
->sum('value');
|
||||
|
||||
$result[] = [
|
||||
if (! isset($result[$month])) {
|
||||
$result[$month] = [];
|
||||
}
|
||||
|
||||
$result[$month][] = [
|
||||
'year' => $date->year,
|
||||
'month' => $date->month,
|
||||
'label' => $date->monthName.' '.$date->year,
|
||||
|
||||
Reference in New Issue
Block a user