string('q')->toString()) { $query->search($search); } if ($kpCode = $request->string('kp_code')->toString()) { $query->where('kp_code', $kpCode); } if ($type = $request->string('question_type')->toString()) { $query->where('question_type', $type); } $stageGrade = $this->normalizeStageGrade((int) $request->input('grade')); if ($stageGrade !== null) { $query->where('grade', $stageGrade); } $min = $request->float('difficulty_min'); $max = $request->float('difficulty_max'); if ($min !== null && $max !== null) { $query->whereBetween('difficulty', [$min, $max]); } $perPage = max(1, min(50, (int) $request->input('per_page', 20))); return response()->json($query->paginate($perPage)); } private function normalizeStageGrade(int $grade): ?int { if ($grade <= 0) { return null; } return $grade <= 9 ? 2 : 3; } }