From 2c94316439053426e8e168ffb7b78921077d3eba Mon Sep 17 00:00:00 2001 From: Irwan Cahyono Date: Wed, 22 Jul 2026 14:38:10 +0700 Subject: [PATCH] petani lahan hapus --- .../Produktivitas/PetaniLahanController.php | 18 +++++++++++-- .../produktivitas/petani/lahan/edit.blade.php | 26 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php b/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php index 47f08ee..3f9f789 100644 --- a/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php +++ b/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php @@ -379,9 +379,23 @@ class PetaniLahanController extends Controller return redirect()->route('petani.lahan-tanaman.index', $petaniId)->with('success', 'Lahan berhasil diperbarui.'); } - public function destroy(string $id) + public function destroy(string $petaniId, string $id) { - // + if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) { + abort(403); + } + + $lahan = Lahan::findOrFail($id); + $lahan->tanaman()->update([ + 'deleted_by' => auth()->id() + ]); + $lahan->tanaman()->delete(); + $lahan->update([ + 'deleted_by' => auth()->id() + ]); + $lahan->delete(); + + return redirect()->route('petani.lahan-tanaman.index', $petaniId)->with('success', 'Lahan berhasil dihapus.'); } public function summary(string $petaniId) diff --git a/resources/views/produktivitas/petani/lahan/edit.blade.php b/resources/views/produktivitas/petani/lahan/edit.blade.php index d0e7d2a..8918274 100644 --- a/resources/views/produktivitas/petani/lahan/edit.blade.php +++ b/resources/views/produktivitas/petani/lahan/edit.blade.php @@ -403,6 +403,7 @@
Kembali +
@@ -1017,7 +1018,7 @@ showConfirmButton: false, timer: 1500 }).then(() => { - window.location.href = "{{ route('petani.index') }}"; + window.location.href = "{{ route('petani.lahan-tanaman.index', ['petani' => $petani->id]) }}"; }); }, error: function(xhr) { @@ -1054,6 +1055,29 @@ } }); + $('#delete-lahan').click(function(e) { + e.preventDefault(); + $.ajax({ + url: "{{ route('petani.lahan-tanaman.destroy', ['petani' => $petani->id, 'lahan_tanaman' => $lahanSelected->id]) }}", + type: "POST", + data: { + _method: 'DELETE', + _token: $('input[name="_token"]').val() + }, + success: function(res) { + Swal.fire({ + icon: 'success', + title: 'Berhasil!', + text: 'Data lahan berhasil dihapus.', + showConfirmButton: false, + timer: 1500 + }).then(() => { + window.location.href = "{{ route('petani.lahan-tanaman.index', ['petani' => $petani->id]) }}"; + }); + } + }); + }); + initWilayah(); initKomoditas(); });