validate([ 'id_produk' => 'required|exists:produk,id', 'foto' => 'required|image|mimes:jpg,jpeg,png|max:2048', ]); $path = $request->file('foto')->store('foto', 'public'); $url = asset('storage/' . $path); $foto = FotoSementara::create([ 'id_produk' => $request->id_produk, 'url' => $url, ]); return response()->json(['message' => 'Foto berhasil disimpan'], 201); } public function hapus($id) { $foto = FotoSementara::findOrFail($id); // Extract the relative path from the URL $relativePath = str_replace(asset('storage') . '/', '', $foto->url); if ($foto->url && Storage::disk('public')->exists($relativePath)) { Storage::disk('public')->delete($relativePath); } $foto->delete(); return response()->json(['message' => 'Foto berhasil dihapus']); } public function getAll($user_id) { $data = FotoSementara::where('id_user', $user_id); return response()->json($data); } public function reset($user_id) { FotoSementara::where('id_user', $user_id)->delete(); return response()->json(['message' => 'Foto sementara berhasil direset']); } }