fix: resolve existing photos preview in Dropzone Lahan&Tanaman
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user