From 45969e0c7732713083904bbc4f0c9271aeeffc62 Mon Sep 17 00:00:00 2001 From: LailaWulandarii Date: Sun, 25 Jan 2026 17:19:49 +0700 Subject: [PATCH] add FotoRequest for improved validation and structure in FotoController --- app/Http/Controllers/Admin/FotoController.php | 103 +++--------------- app/Http/Requests/Admin/FotoRequest.php | 72 ++++++++++++ 2 files changed, 85 insertions(+), 90 deletions(-) create mode 100644 app/Http/Requests/Admin/FotoRequest.php diff --git a/app/Http/Controllers/Admin/FotoController.php b/app/Http/Controllers/Admin/FotoController.php index 648f987..c3c95dd 100644 --- a/app/Http/Controllers/Admin/FotoController.php +++ b/app/Http/Controllers/Admin/FotoController.php @@ -5,15 +5,11 @@ use App\Http\Controllers\Controller; use App\Models\Additional; use App\Models\PaketFoto; -use Illuminate\Http\Request; -use Illuminate\Support\Facades\Validator; +use App\Http\Requests\Admin\FotoRequest; // Gunakan Request baru use Illuminate\Support\Facades\Storage; class FotoController extends Controller { - /** - * Display a listing of the resource. - */ public function index() { $foto = PaketFoto::latest()->get(); @@ -21,100 +17,30 @@ public function index() return view('admin.paket-foto.index', compact('foto', 'additional')); } - public function store(Request $request) + public function store(FotoRequest $request) { - $validator = Validator::make($request->all(), [ - 'nama' => 'required|string|min:3|max:100', - 'harga' => 'required|numeric|min:0', - 'durasi' => 'required|integer|min:0', - 'deskripsi' => 'required|string|min:10', - 'foto' => 'required|image|mimes:jpeg,png,jpg|max:2048', - ], [ - 'required' => 'Kolom :attribute wajib diisi.', - 'string' => 'Input :attribute harus berupa teks valid.', - 'integer' => 'Input :attribute harus berupa angka valid.', - 'max' => ':attribute melebihi batas, maksimal :max karakter/KB.', - 'image' => ':attribute harus berupa file gambar (foto).', - 'mimes' => 'Format :attribute tidak didukung. Gunakan format: jpeg, png, atau jpg.', - 'foto.max' => 'Ukuran :attribute maksimal 2MB.', - 'min' => ':attribute minimal harus :min karakter/nilai.', - 'numeric' => ':attribute harus berupa angka.', - - ], [ - 'nama' => 'nama paket', - 'harga' => 'harga paket', - 'durasi' => 'durasi paket', - 'foto' => 'foto paket', - ]); - if ($validator->fails()) { - return redirect()->back() - ->withErrors($validator) - ->withInput() - ->with('error_modal', 'createFoto'); + $data = $request->validated(); + if ($request->hasFile('foto')) { + $file = $request->file('foto'); + $filename = time() . '_' . $file->getClientOriginalName(); + $path = $file->storeAs('img/foto', $filename, 'public'); + $data['foto'] = $path; } - if (!$request->hasFile('foto')) { - return redirect()->back() - ->with('error', 'File foto tidak ditemukan!') - ->withInput(); - } - $file = $request->file('foto'); - $filename = time() . '_' . $file->getClientOriginalName(); - $path = $file->storeAs('img/foto', $filename, 'public'); - if (!$path) { - return redirect()->back() - ->with('error', 'Gagal upload file!') - ->withInput(); - } - PaketFoto::create([ - 'nama' => $request->nama, - 'harga' => $request->harga, - 'durasi' => $request->durasi, - 'deskripsi' => $request->deskripsi, - 'foto' => $path, - ]); + PaketFoto::create($data); return redirect()->back()->with('success', 'Paket foto baru berhasil ditambahkan!'); } - public function update(Request $request, string $id) + public function update(FotoRequest $request, string $id) { $paket = PaketFoto::findOrFail($id); - $validator = Validator::make($request->all(), [ - 'nama' => 'required|string|min:3|max:100', - 'harga' => 'required|numeric|min:0', - 'durasi' => 'required|integer|min:0', - 'deskripsi' => 'required|string|min:10', - 'foto' => 'nullable|image|mimes:jpeg,png,jpg|max:2048', - ], [ - 'required' => 'Kolom :attribute wajib diisi.', - 'string' => 'Input :attribute harus berupa teks valid.', - 'integer' => 'Input :attribute harus berupa angka valid.', - 'max' => ':attribute melebihi batas, maksimal :max karakter/KB.', - 'image' => ':attribute harus berupa file gambar (foto).', - 'mimes' => 'Format :attribute tidak didukung. Gunakan format: jpeg, png, atau jpg.', - 'foto.max' => 'Ukuran :attribute maksimal 2MB.', - 'min' => ':attribute minimal harus :min karakter/nilai.', - 'numeric' => ':attribute harus berupa angka.', - ], [ - 'nama' => 'nama paket', - 'harga' => 'harga paket', - 'durasi' => 'durasi paket', - 'foto' => 'foto paket', - ]); - if ($validator->fails()) { - return redirect()->back() - ->withErrors($validator) - ->withInput() - ->with('error_id_foto', $id); - } - $data = $request->only(['nama', 'harga', 'durasi', 'deskripsi']); + $data = $request->validated(); if ($request->hasFile('foto')) { if ($paket->foto) { Storage::disk('public')->delete($paket->foto); } $file = $request->file('foto'); $filename = time() . '_' . $file->getClientOriginalName(); - $path = $file->storeAs('img/foto', $filename, 'public'); - $data['foto'] = $path; + $data['foto'] = $file->storeAs('img/foto', $filename, 'public'); } $paket->update($data); return redirect()->back()->with('success', 'Paket foto berhasil diperbarui!'); @@ -122,14 +48,11 @@ public function update(Request $request, string $id) public function destroy(string $id) { - // Cari data berdasarkan primary key id_paket - $paket = PaketFoto::where('id_paket', $id)->firstOrFail(); - + $paket = PaketFoto::findOrFail($id); if ($paket->foto) { Storage::disk('public')->delete($paket->foto); } $paket->delete(); - return redirect()->back()->with('success', 'Paket foto dan filenya berhasil dihapus!'); } } diff --git a/app/Http/Requests/Admin/FotoRequest.php b/app/Http/Requests/Admin/FotoRequest.php new file mode 100644 index 0000000..afd4d3a --- /dev/null +++ b/app/Http/Requests/Admin/FotoRequest.php @@ -0,0 +1,72 @@ + 'required|string|min:3|max:100', + 'harga' => 'required|numeric|min:0', + 'durasi' => 'required|integer|min:0', + 'deskripsi' => 'required|string|min:10', + // Foto wajib saat Tambah (POST), opsional saat Edit (PUT) + 'foto' => $this->isMethod('post') + ? 'required|image|mimes:jpeg,png,jpg|max:2048' + : 'nullable|image|mimes:jpeg,png,jpg|max:2048', + ]; + } + + public function messages(): array + { + return [ + 'required' => 'Kolom :attribute wajib diisi.', + 'string' => 'Input :attribute harus berupa teks valid.', + 'integer' => 'Input :attribute harus berupa angka valid.', + 'numeric' => ':attribute harus berupa angka.', + 'min' => ':attribute minimal harus :min karakter/nilai.', + 'max' => ':attribute melebihi batas, maksimal :max karakter/KB.', + 'image' => ':attribute harus berupa file gambar (foto).', + 'mimes' => 'Format :attribute tidak didukung (jpeg, png, jpg).', + 'foto.max' => 'Ukuran :attribute maksimal 2MB.', + ]; + } + + public function attributes(): array + { + return [ + 'nama' => 'nama paket', + 'harga' => 'harga paket', + 'durasi' => 'durasi paket', + 'foto' => 'foto paket', + ]; + } + + /** + * Penanganan khusus untuk Modal di Admin + */ + protected function failedValidation(Validator $validator) + { + $redirect = redirect()->back()->withErrors($validator)->withInput(); + + if ($this->isMethod('post')) { + // Trigger modal tambah foto + $redirect->with('error_modal', 'createFoto'); + } else { + // Trigger modal edit berdasarkan ID paket + $redirect->with('error_id_foto', $this->route('foto')); + } + + throw new HttpResponseException($redirect); + } +}