diff --git a/app/Http/Controllers/Web/Produktivitas/PetaniController.php b/app/Http/Controllers/Web/Produktivitas/PetaniController.php index cebfe94..7551b44 100644 --- a/app/Http/Controllers/Web/Produktivitas/PetaniController.php +++ b/app/Http/Controllers/Web/Produktivitas/PetaniController.php @@ -63,9 +63,8 @@ class PetaniController extends Controller ]) ->orderBy('created_at', 'desc'); - if ($request->filled('search')) { - $search = Str::lower($request->search); - + $search = $request->filled('search') ? Str::lower($request->search) : null; + if ($search) { $query->where(function ($query) use ($search) { $query->whereRaw('lower(nama) like ?', ["%{$search}%"]) ->orWhereRaw('lower(ktp) like ?', ["%{$search}%"]); @@ -111,9 +110,42 @@ class PetaniController extends Controller $desaOptions = $request->filled('kecamatan_id') ? DesaKelurahan::select('id', 'nama')->where('kecamatan_id', $request->kecamatan_id)->orderBy('nama')->get() : collect(); - $totalLahan = Lahan::count(); - $totalLuasLahan = Lahan::sum('luas_lahan'); - $totalTanaman = Tanaman::count(); + $lahanSummaryQuery = Lahan::query() + ->when($request->filled('komoditas_id'), function ($query) use ($request) { + $query->where('komoditas_id', $request->komoditas_id); + }) + ->when($filters['kabupaten_kota_id'], function ($query) use ($filters) { + $query->whereHas('desaKelurahan.kecamatan', function ($kecamatan) use ($filters) { + $kecamatan->where('kabupaten_kota_id', $filters['kabupaten_kota_id']); + }); + }) + ->when($request->filled('kecamatan_id'), function ($query) use ($request) { + $query->whereHas('desaKelurahan', function ($desa) use ($request) { + $desa->where('kecamatan_id', $request->kecamatan_id); + }); + }) + ->when($request->filled('desa_kelurahan_id'), function ($query) use ($request) { + $query->where('desa_kelurahan_id', $request->desa_kelurahan_id); + }) + ->when($request->filled('search'), function ($query) use ($search) { + $query->whereHas('petani', function ($petani) use ($search) { + $petani->whereRaw('lower(nama) like ?', ["%{$search}%"]) + ->orWhereRaw('lower(ktp) like ?', ["%{$search}%"]); + }); + }) + ->when($userRole === 'fasilitator', function ($query) { + $query->whereHas('petani', function ($petani) { + $petani->where('created_by', auth()->id()); + }); + }); + + $totalLahan = (clone $lahanSummaryQuery)->count(); + $totalLuasLahan = (clone $lahanSummaryQuery)->sum('luas_lahan'); + $totalTanaman = Tanaman::whereIn('lahan_id', (clone $lahanSummaryQuery)->select('id')) + ->when($request->filled('komoditas_id'), function ($query) use ($request) { + $query->where('komoditas_id', $request->komoditas_id); + }) + ->count(); $case = " CASE diff --git a/resources/views/produktivitas/petani/index.blade.php b/resources/views/produktivitas/petani/index.blade.php index b977174..707e4dc 100644 --- a/resources/views/produktivitas/petani/index.blade.php +++ b/resources/views/produktivitas/petani/index.blade.php @@ -106,8 +106,8 @@