get(); $petani = Petani::pluck('id'); $lahan = Lahan::whereIn('petani_id', $petani)->get(); $petaniSummary = Petani::select( 'komoditas_id', DB::raw('COUNT(*) as total') ) ->groupBy('komoditas_id') ->get(); $lahanSummary = Lahan::select( 'komoditas_id', DB::raw('COUNT(*) as total'), DB::raw('SUM(luas_lahan) as luas_lahan') ) ->whereIn('petani_id', $petani) ->groupBy('komoditas_id') ->get(); $tanamanSummary = Tanaman::select( 'komoditas_id', DB::raw('COUNT(*) as total') ) ->whereIn('lahan_id', $lahan->pluck('id')) ->groupBy('komoditas_id') ->get(); $case = " CASE WHEN skor_kesiapan_adopsi >= 4.0 THEN 'Penggerak' WHEN skor_kesiapan_adopsi >= 3.0 THEN 'Perintis' WHEN skor_kesiapan_adopsi >= 2.0 THEN 'Pemerhati' WHEN skor_kesiapan_adopsi >= 1.0 THEN 'Pemula' ELSE 'Belum Lengkap' END "; $skorSummary = SkorPetani::selectRaw("$case AS segmen, COUNT(*) AS total") ->whereIn('petani_id', $petani) ->groupByRaw($case) ->get(); $skorKomoditasSummary = SkorPetani::query() ->join('petani', 'petani_skor.petani_id', '=', 'petani.id') ->selectRaw(" petani.komoditas_id, $case AS segmen, COUNT(petani.id) AS total ") ->whereIn('petani_skor.petani_id', $petani) ->groupBy('petani.komoditas_id') ->groupByRaw($case) ->get(); return view('dashboard.petani', compact('title', 'komoditas', 'petaniSummary', 'lahanSummary', 'tanamanSummary', 'skorSummary', 'skorKomoditasSummary' )); } }