Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4089d72b3 | ||
|
|
01990ccec1 | ||
|
|
4e41246382 |
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Dashboard;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use App\Models\Pertanian\Petani;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
use App\Models\Pertanian\Lahan;
|
||||
use App\Models\Pertanian\PendapatanKomoditas;
|
||||
|
||||
class DemografiPetaniController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$totalPetani = Petani::count();
|
||||
$petaniLakilaki = Petani::where('jenis_kelamin', 1)->count();
|
||||
$petaniPerempuan = Petani::where('jenis_kelamin', 0)->count();
|
||||
$totalKomoditas = Petani::whereNotNull('komoditas_id')
|
||||
->distinct()
|
||||
->count('komoditas_id');
|
||||
$totalLuasLahan = Lahan::has('petani')
|
||||
->sum('luas_lahan');
|
||||
$ratarataLuasLahan = Lahan::has('petani')
|
||||
->avg('luas_lahan');
|
||||
|
||||
$umurKurang30 = Petani::whereNotNull('tanggal_lahir')->whereRaw("EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) < 30")->count();
|
||||
$umur30sampai39 = Petani::whereNotNull('tanggal_lahir')->whereRaw("EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 30 AND 39")->count();
|
||||
$umur40sampai49 = Petani::whereNotNull('tanggal_lahir')->whereRaw("EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 40 AND 49")->count();
|
||||
$umur50sampai59 = Petani::whereNotNull('tanggal_lahir')->whereRaw("EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 50 AND 59")->count();
|
||||
$umur60keatas = Petani::whereNotNull('tanggal_lahir')->whereRaw("EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) >= 60")->count();
|
||||
|
||||
$pendidikanCounts = Petani::whereNotNull('pendidikan_terakhir')
|
||||
->select('pendidikan_terakhir', DB::raw('COUNT(*) as total'))
|
||||
->groupBy('pendidikan_terakhir')
|
||||
->pluck('total', 'pendidikan_terakhir');
|
||||
|
||||
$pendidikan = [
|
||||
'tidak_sekolah' => (int) ($pendidikanCounts['01_tidak_sekolah'] ?? 0),
|
||||
'sd' => (int) ($pendidikanCounts['02_sd'] ?? 0),
|
||||
'smp' => (int) ($pendidikanCounts['03_smp'] ?? 0),
|
||||
'sma' => (int) ($pendidikanCounts['04_sma'] ?? 0),
|
||||
'diploma' => (int) ($pendidikanCounts['05_diploma'] ?? 0),
|
||||
|
||||
's1_keatas' =>
|
||||
(int) ($pendidikanCounts['06_sarjana'] ?? 0) +
|
||||
(int) ($pendidikanCounts['07_magister'] ?? 0),
|
||||
];
|
||||
|
||||
$komoditasUtama = Komoditas::query()
|
||||
->whereHas('petani')
|
||||
->withCount('petani')
|
||||
->orderByDesc('petani_count')
|
||||
->get(['id', 'nama']);
|
||||
|
||||
$luasLahanPerPetani = Lahan::has('petani')
|
||||
->select('petani_id')
|
||||
->selectRaw('SUM(luas_lahan) as total_luas')
|
||||
->groupBy('petani_id')
|
||||
->get();
|
||||
|
||||
$luasLahan = [
|
||||
'kurang_05' => $luasLahanPerPetani->filter(fn ($item) => $item->total_luas < 5000)->count(),
|
||||
'05_1' => $luasLahanPerPetani->filter(fn ($item) => $item->total_luas >= 5000 && $item->total_luas <= 10000)->count(),
|
||||
'1_2' => $luasLahanPerPetani->filter(fn ($item) => $item->total_luas > 10000 && $item->total_luas <= 20000)->count(),
|
||||
'lebih_2' => $luasLahanPerPetani->filter(fn ($item) => $item->total_luas > 20000)->count(),
|
||||
];
|
||||
|
||||
$pendapatanPerPetani = PendapatanKomoditas::with('sosialEkonomi.petani')->get()
|
||||
->filter(fn ($item) => $item->sosialEkonomi?->petani)
|
||||
->groupBy(fn ($item) => $item->sosialEkonomi->petani_id)
|
||||
->map(fn ($items) => $items->sum('pendapatan'));
|
||||
|
||||
$penghasilan = [
|
||||
'kurang_1' => $pendapatanPerPetani->filter(fn ($value) => $value < 1000000)->count(),
|
||||
'1_3' => $pendapatanPerPetani->filter(fn ($value) => $value >= 1000000 && $value <= 3000000)->count(),
|
||||
'3_5' => $pendapatanPerPetani->filter(fn ($value) => $value > 3000000 && $value <= 5000000)->count(),
|
||||
'5_10' => $pendapatanPerPetani->filter(fn ($value) => $value > 5000000 && $value <= 10000000)->count(),
|
||||
'lebih_10' => $pendapatanPerPetani->filter(fn ($value) => $value > 10000000)->count(),
|
||||
];
|
||||
|
||||
$data = [
|
||||
'total_petani' => $totalPetani,
|
||||
'petani_lakilaki' => $petaniLakilaki,
|
||||
'petani_perempuan' => $petaniPerempuan,
|
||||
'total_komoditas' => $totalKomoditas,
|
||||
'total_luas_lahan' => $totalLuasLahan,
|
||||
'ratarata_luas_lahan' => $ratarataLuasLahan,
|
||||
'kelompok_umur' => [
|
||||
'kurang_30' => $umurKurang30,
|
||||
'30_39' => $umur30sampai39,
|
||||
'40_49' => $umur40sampai49,
|
||||
'50_59' => $umur50sampai59,
|
||||
'60_keatas' => $umur60keatas,
|
||||
],
|
||||
'pendidikan' => $pendidikan,
|
||||
'komoditas_utama' => [
|
||||
'labels' => $komoditasUtama
|
||||
->pluck('nama')
|
||||
->values(),
|
||||
|
||||
'series' => $komoditasUtama
|
||||
->pluck('petani_count')
|
||||
->map(fn ($value) => (int) $value)
|
||||
->values(),
|
||||
],
|
||||
'luas_lahan' => $luasLahan,
|
||||
'penghasilan' => $penghasilan,
|
||||
];
|
||||
|
||||
$daftarPetani = Petani::with([
|
||||
'komoditas'
|
||||
])
|
||||
->withSum('lahans as total_luas_lahan', 'luas_lahan')
|
||||
->orderBy('id')
|
||||
->paginate(10);
|
||||
// dd($daftarPetani);
|
||||
return view('dashboard.demografi-petani', compact('data', 'daftarPetani'));
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -46,6 +46,7 @@ Route::middleware('auth')
|
||||
// Dashboard
|
||||
Route::get('/dashboard/petani', [App\Http\Controllers\Web\Dashboard\ProfilPetaniController::class, 'index'])->name('dashboard.petani');
|
||||
Route::get('/dashboard/bumdes', [App\Http\Controllers\Web\Bumdes\PengurusBumdesController::class, 'index'])->name('dashboard.bumdes');
|
||||
Route::get('/dashboard/petani/demografi', [App\Http\Controllers\Web\Dashboard\DemografiPetaniController::class, 'index'])->name('dashboard.demografi-petani');
|
||||
|
||||
Route::get('/dashboard/indeks-petani', [App\Http\Controllers\Web\Dashboard\IndeksPetaniController::class, 'index'])->name('dashboard.indeks-petani');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user