sortByDesc('id'); $hewan = Hewan::all()->sortByDesc('id'); return view('admin2.pages.hewan', [ 'hewan' => $hewan, 'kategori' => $kategori ]); } public function store(Request $request) { try { $request->validate([ 'name' => 'required|string|max:255', 'description' => 'required|string', 'foto' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', 'kategori_id' => 'required|exists:kategori,id' ], [ 'name.required' => 'Nama hewan harus diisi', 'name.string' => 'Nama hewan harus berupa string', 'name.max' => 'Nama hewan maksimal 255 karakter', 'description.required' => 'Deskripsi hewan harus diisi', 'description.string' => 'Deskripsi hewan harus berupa string', 'foto.required' => 'Foto hewan harus diisi', 'foto.image' => 'Foto hewan harus berupa gambar', 'foto.mimes' => 'Foto hewan harus berupa gambar dengan format jpeg, png, jpg, gif, atau svg', 'foto.max' => 'Foto hewan maksimal 2048 kilobytes', 'kategori_id.required' => 'Kategori hewan harus diisi', 'kategori_id.exists' => 'Kategori hewan tidak ditemukan' ]); $image = $request->file('foto'); $image_name = time() . '.' . $image-> extension(); $image_path = public_path('images/foto/') . $image_name; $image->move(public_path('images/foto/'), $image_name); $hewan = new Hewan(); $hewan->name = $request->name; $hewan->description = $request->description; $hewan->foto = $image_name; $hewan->kategori_id = $request->kategori_id; $hewan->save(); return redirect()->back()->with('success', 'Hewan berhasil ditambahkan'); } catch (\Exception $e) { return redirect()->back()->with('error', $e->getMessage()); } } public function update(Request $request, $id) { try { $request->validate([ 'name' => 'required|string|max:255', 'description' => 'required|string', 'foto' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048', 'kategori_id' => 'required|exists:kategori,id' ], [ 'name.required' => 'Nama hewan harus diisi', 'name.string' => 'Nama hewan harus berupa string', 'name.max' => 'Nama hewan maksimal 255 karakter', 'description.required' => 'Deskripsi hewan harus diisi', 'description.string' => 'Deskripsi hewan harus berupa string', 'foto.image' => 'Foto hewan harus berupa gambar', 'foto.mimes' => 'Foto hewan harus berupa gambar dengan format jpeg, png, jpg, gif, atau svg', 'foto.max' => 'Foto hewan maksimal 2048 kilobytes', 'kategori_id.required' => 'Kategori hewan harus diisi', 'kategori_id.exists' => 'Kategori hewan tidak ditemukan' ]); $hewan = Hewan::find($id); $hewan->name = $request->name; $hewan->description = $request->description; $hewan->kategori_id = $request->kategori_id; if ($request->hasFile('foto')) { $old_image_path = public_path('images/foto/') . $hewan->foto; if (file_exists($old_image_path)) { unlink($old_image_path); } $image = $request->file('foto'); $image_name = time() . '.' . $image->extension(); $image_path = public_path('images/foto/') . $image_name; $image->move(public_path('images/foto/'), $image_name); $hewan->foto = $image_name; } $hewan->save(); return redirect()->back()->with('success', 'Hewan berhasil diubah'); } catch (\Exception $e) { return redirect()->back()->with('error', $e->getMessage()); } } public function destroy($id) { try { $hewan = Hewan::find($id); $image_path = public_path('images/foto/') . $hewan->foto; if (file_exists($image_path)) { unlink($image_path); } $hewan->delete(); return redirect()->back()->with('success', 'Hewan berhasil dihapus'); } catch (\Exception $e) { return redirect()->back()->with('error', $e->getMessage()); } } }