From 8afe7f9b38c2054cb1096b78665b4d91470b8656 Mon Sep 17 00:00:00 2001 From: althaf Date: Tue, 21 Jul 2026 11:49:32 +0700 Subject: [PATCH] solve view-controller dropzone --- .../Produktivitas/PetaniLahanController.php | 18 +++++++++++++++++- .../produktivitas/petani/lahan/edit.blade.php | 7 ++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php b/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php index f597537..47f08ee 100644 --- a/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php +++ b/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php @@ -302,6 +302,22 @@ class PetaniLahanController extends Controller $fotoLama = []; } + if ($request->has('deleted_photos') && is_array($request->deleted_photos)) { + foreach ($request->deleted_photos as $deletedPath) { + + if (Storage::disk('public')->exists($deletedPath)) { + Storage::disk('public')->delete($deletedPath); + } + + $fotoLama = array_filter($fotoLama, function($item) use ($deletedPath) { + $currentPath = is_array($item) ? ($item['path'] ?? '') : $item; + return $currentPath !== $deletedPath; + }); + } + } + + $fotoLama = array_values($fotoLama); + if ($request->hasFile('foto')) { foreach ($request->file('foto') as $file) { $path = $file->store('lahan', 'public'); @@ -315,7 +331,7 @@ class PetaniLahanController extends Controller unset($validated['foto']); $lahan->update([...$validated, - 'foto' => count($fotoLama) > 0 ? $fotoLama : null, + 'foto' => count($fotoLama) > 0 ? json_encode($fotoLama) : null, 'updated_by' => auth()->id()] ); diff --git a/resources/views/produktivitas/petani/lahan/edit.blade.php b/resources/views/produktivitas/petani/lahan/edit.blade.php index 4e1d1e5..391dd8e 100644 --- a/resources/views/produktivitas/petani/lahan/edit.blade.php +++ b/resources/views/produktivitas/petani/lahan/edit.blade.php @@ -763,7 +763,8 @@ let mockFile = { name: "Foto Lahan " + (index + 1), size: 1024, - id: index, + id: index, + file_path: pathStr, isServerFile: true, accepted: true }; @@ -782,7 +783,7 @@ let deleteInput = $('').attr({ type: 'hidden', name: 'deleted_photos[]', - value: file.id + value: file.file_path }); $('#deleted-photos-container').append(deleteInput); } @@ -811,7 +812,7 @@ let deleteInput = $('').attr({ type: 'hidden', name: 'deleted_photos[]', - value: fileToReplace.id + value: fileToReplace.file_path }); $('#deleted-photos-container').append(deleteInput); }