diff --git a/app/Http/Controllers/Api/Produktivitas/LahanController.php b/app/Http/Controllers/Api/Produktivitas/LahanController.php index 8a3e12e..6ccd277 100644 --- a/app/Http/Controllers/Api/Produktivitas/LahanController.php +++ b/app/Http/Controllers/Api/Produktivitas/LahanController.php @@ -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(); } diff --git a/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php b/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php index c4febdb..873bbba 100644 --- a/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php +++ b/app/Http/Controllers/Web/Produktivitas/PetaniLahanController.php @@ -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) diff --git a/resources/views/produktivitas/petani/lahan/add.blade.php b/resources/views/produktivitas/petani/lahan/add.blade.php index 5103d90..dc8b203 100644 --- a/resources/views/produktivitas/petani/lahan/add.blade.php +++ b/resources/views/produktivitas/petani/lahan/add.blade.php @@ -389,7 +389,7 @@ - + @error('file')