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
@@ -16,7 +16,7 @@ use App\Enums\StatusLahan;
class LahanController extends Controller
{
/**
/**
* Display a listing of the resource.
*/
public function index(Request $request): AnonymousResourceCollection
@@ -97,11 +97,12 @@ class LahanController extends Controller
'nama' => ['required', 'string', 'max:255'],
'status_kepemilikan' => ['required', new Enum(StatusLahan::class)],
'desa_kelurahan_id' => ['nullable', 'numeric', 'exists:master_desa_kelurahan,id'],
'path' => ['nullable', 'array'],
'path.*' => ['array'],
'path' => ['nullable', 'string'],
'luas_lahan' => ['nullable', 'numeric'],
'uid' => ['required', 'string'],
'profile_id' => $profileRule,
'foto' => ['nullable', 'array'],
'foto.*' => ['image', 'mimes:jpeg,png,jpg', 'max:5120'], // Max 5MB
]);
$targetProfileId = $user->role === 'fasilitator'
@@ -120,10 +121,22 @@ class LahanController extends Controller
? $profile->desa_kelurahan_id
: ($validated['desa_kelurahan_id'] ?? $profile->desa_kelurahan_id);
$fotoPaths = [];
if ($request->hasFile('foto')) {
foreach ($request->file('foto') as $file) {
$fotoPaths[] = $file->store('foto_lahan', 'public');
}
}
$lahan = Lahan::create([
...$validated,
'profile_id' => $profile->id,
'nama' => $validated['nama'],
'status_kepemilikan' => $validated['status_kepemilikan'],
'desa_kelurahan_id' => $desaKelurahanId,
'path' => $validated['path'] ?? null,
'luas_lahan' => $validated['luas_lahan'] ?? null,
'uid' => $validated['uid'],
'profile_id' => $profile->id,
'foto' => $fotoPaths,
]);
return (new LahanResource($lahan))
@@ -153,7 +166,7 @@ class LahanController extends Controller
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id): JsonResponse
public function update(Request $request, string $id): JsonResponse
{
if (!Gate::any(['petani', 'fasilitator','super_admin'])) {
abort(403);
@@ -173,9 +186,26 @@ class LahanController extends Controller
'path' => ['nullable', 'array'],
'path.*' => ['array'],
'luas_lahan' => ['nullable', 'numeric'],
'foto' => ['nullable', 'array'],
'foto.*' => ['image', 'mimes:jpeg,png,jpg', 'max:5120'],
]);
$lahan->update($validated);
$fotoPaths = $lahan->foto ?? [];
if ($request->hasFile('foto')) {
foreach ($request->file('foto') as $file) {
$fotoPaths[] = $file->store('foto_lahan', 'public');
}
}
$lahan->update([
'nama' => $validated['nama'],
'status_kepemilikan' => $validated['status_kepemilikan'],
'desa_kelurahan_id' => $validated['desa_kelurahan_id'] ?? $lahan->desa_kelurahan_id,
'path' => $validated['path'] ?? null,
'luas_lahan' => $validated['luas_lahan'] ?? null,
'foto' => $fotoPaths,
]);
return (new LahanResource($lahan))->response();
}
@@ -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)