sum('luas_lahan'); $totalpetani = Profile::where('role', 'petani')->count(); $totallahan = Lahan::whereNotNull('path')->count(); $petaniList = Profile::where('role', 'petani') ->with(['lahan.tanaman.komoditas', 'desaKelurahan']) ->paginate(20); $allLahan = Lahan::whereNotNull('path') ->with(['profile', 'tanaman.komoditas', 'desaKelurahan.kecamatan.kabupatenKota.provinsi']) ->get(); $geoProvinsi = []; $geoKabupaten = []; $geoKecamatan = []; $geoDesa = []; $geoLahan = []; foreach ($allLahan as $lahan) { $desa = $lahan->desaKelurahan; $kec = $desa ? $desa->kecamatan : null; $kab = $kec ? $kec->kabupatenKota : null; $prov = $kab ? $kab->provinsi : null; if ($prov && $prov->path) { if (!isset($geoProvinsi[$prov->id])) { $geoProvinsi[$prov->id] = ['id' => $prov->id, 'nama' => $prov->nama, 'polygon' => $prov->path, 'total_lahan' => 0, 'total_luas' => 0]; } $geoProvinsi[$prov->id]['total_lahan']++; $geoProvinsi[$prov->id]['total_luas'] += $lahan->luas_lahan; } if ($kab && $kab->path) { if (!isset($geoKabupaten[$kab->id])) { $geoKabupaten[$kab->id] = ['id' => $kab->id, 'parent_id' => $prov->id ?? null, 'nama' => $kab->nama, 'polygon' => $kab->path, 'total_lahan' => 0, 'total_luas' => 0]; } $geoKabupaten[$kab->id]['total_lahan']++; $geoKabupaten[$kab->id]['total_luas'] += $lahan->luas_lahan; } if ($kec && $kec->path) { if (!isset($geoKecamatan[$kec->id])) { $geoKecamatan[$kec->id] = ['id' => $kec->id, 'parent_id' => $kab->id ?? null, 'nama' => $kec->nama, 'polygon' => $kec->path, 'total_lahan' => 0, 'total_luas' => 0]; } $geoKecamatan[$kec->id]['total_lahan']++; $geoKecamatan[$kec->id]['total_luas'] += $lahan->luas_lahan; } if ($desa && $desa->path) { if (!isset($geoDesa[$desa->id])) { $geoDesa[$desa->id] = ['id' => $desa->id, 'parent_id' => $kec->id ?? null, 'nama' => $desa->nama, 'polygon' => $desa->path, 'total_lahan' => 0, 'total_luas' => 0]; } $geoDesa[$desa->id]['total_lahan']++; $geoDesa[$desa->id]['total_luas'] += $lahan->luas_lahan; } $geoLahan[] = [ 'id' => $lahan->id, 'parent_id' => $desa->id ?? null, 'polygon' => $lahan->path, 'nama_petani' => $lahan->profile->nama ?? 'N/A', 'luas' => $lahan->luas_lahan, 'tanaman' => $lahan->tanaman->map(fn($t) => $t->komoditas->nama ?? null)->filter()->unique()->values() ]; } return view('dashboard.produktivitas', [ 'title' => $title, 'petaniList' => $petaniList, 'totalpetani' => $totalpetani, 'totallahan' => $totallahan, 'luasTotal' => $luasTotal, 'geoProvinsi' => array_values($geoProvinsi), 'geoKabupaten' => array_values($geoKabupaten), 'geoKecamatan' => array_values($geoKecamatan), 'geoDesa' => array_values($geoDesa), 'geoLahan' => $geoLahan ]); } }