input('per_page', 7); $perPage = in_array($perPage, [5, 7]) ? $perPage : 7; $search = trim((string) $request->input('search', '')); $query = ProspekKerja::with('jurusan')->orderBy('id', 'desc'); if ($search !== '') { $query->where(function ($q) use ($search) { $q->where('nama_profesi', 'like', "%{$search}%") ->orWhere('range_gaji', 'like', "%{$search}%") ->orWhere('deskripsi_pekerjaan', 'like', "%{$search}%") ->orWhereHas('jurusan', function ($jurusan) use ($search) { $jurusan->where('nama_jurusan', 'like', "%{$search}%"); }); }); } $prospek = $query->paginate($perPage); return response()->json([ 'success' => true, 'data' => $prospek->items(), 'current_page' => $prospek->currentPage(), 'last_page' => $prospek->lastPage(), 'per_page' => $prospek->perPage(), 'total' => $prospek->total(), 'from' => $prospek->firstItem(), 'to' => $prospek->lastItem(), ], 200); } public function store(Request $request) { $validator = Validator::make($request->all(), [ 'jurusan_id' => 'required|exists:jurusans,id', 'nama_profesi' => 'required|string|max:255', 'range_gaji' => 'nullable|string|max:255', 'deskripsi_pekerjaan' => 'nullable|string', ]); if ($validator->fails()) { return response()->json(['success' => false, 'errors' => $validator->errors()], 422); } $prospek = ProspekKerja::create($request->all()); return response()->json([ 'success' => true, 'message' => 'Prospek Kerja berhasil ditambahkan', 'data' => $prospek ], 201); } public function show($id) { $prospek = ProspekKerja::find($id); if (!$prospek) { return response()->json(['success' => false, 'message' => 'Data tidak ditemukan'], 404); } return response()->json(['success' => true, 'data' => $prospek], 200); } public function update(Request $request, $id) { $prospek = ProspekKerja::find($id); if (!$prospek) { return response()->json(['success' => false, 'message' => 'Data tidak ditemukan'], 404); } $validator = Validator::make($request->all(), [ 'jurusan_id' => 'required|exists:jurusans,id', 'nama_profesi' => 'required|string|max:255', 'range_gaji' => 'nullable|string|max:255', 'deskripsi_pekerjaan' => 'nullable|string', ]); if ($validator->fails()) { return response()->json(['success' => false, 'errors' => $validator->errors()], 422); } $prospek->update($request->all()); return response()->json([ 'success' => true, 'message' => 'Prospek Kerja berhasil diperbarui', 'data' => $prospek ], 200); } public function destroy($id) { $prospek = ProspekKerja::find($id); if (!$prospek) { return response()->json(['success' => false, 'message' => 'Data tidak ditemukan'], 404); } $prospek->delete(); return response()->json(['success' => true, 'message' => 'Prospek Kerja berhasil dihapus'], 200); } }