fix: resolve existing photos preview in Dropzone Lahan&Tanaman

This commit is contained in:
2026-07-20 16:36:12 +07:00
parent 837e542851
commit 3e1d3a33ee
4 changed files with 327 additions and 32 deletions
@@ -256,9 +256,28 @@ class PetaniLahanController extends Controller
$tanamanCount = Tanaman::where('lahan_id', $lahan->id)->count();
$lahanCount = Lahan::where('petani_id', $petaniId)->count();
$fotoLama = $lahan->foto ?? [];
if (is_string($fotoLama)) {
$fotoLama = json_decode($fotoLama, true);
}
if (!is_array($fotoLama)) {
$fotoLama = [];
}
if ($request->hasFile('foto')) {
foreach ($request->file('foto') as $file) {
$path = $file->store('lahan', 'public');
$fotoLama[] = [
'path' => $path,
'file' => $file->getClientOriginalName(),
];
}
}
unset($validated['foto']);
$lahan->update([...$validated,
//'nama' => 'Lahan ' . $komoditas->nama . ' #' . ($lahanCount + 1),
//'petani_id' => $petaniId,
'foto' => count($fotoLama) > 0 ? $fotoLama : null,
'updated_by' => auth()->id()]
);
@@ -293,7 +312,15 @@ class PetaniLahanController extends Controller
});
}
return redirect()->route('petani.lahan-tanaman.index', $petaniId)->with('success', 'Lahan berhasil ditambahkan.');
if ($request->ajax()) {
return response()->json([
'success' => true,
'redirect' => route('petani.lahan-tanaman.index', $petaniId),
'message' => 'Lahan berhasil diupdate.'
]);
}
return redirect()->route('petani.lahan-tanaman.index', $petaniId)->with('success', 'Lahan berhasil diperbarui.');
}
public function destroy(string $id)