input('count', 10))); $kpCodes = (array) $request->input('kp_codes', []); $types = (array) $request->input('question_types', []); $query = Question::query(); if (!empty($kpCodes)) { $query->whereIn('kp_code', $kpCodes); } if (!empty($types)) { $query->whereIn('question_type', $types); } $stageGrade = $this->normalizeStageGrade((int) $request->input('grade')); if ($stageGrade !== null) { $query->where('grade', $stageGrade); } $questions = $query->inRandomOrder()->limit($count)->get(); return response()->json([ 'count' => $questions->count(), 'data' => $questions, ]); } private function normalizeStageGrade(int $grade): ?int { if ($grade <= 0) { return null; } return $grade <= 9 ? 2 : 3; } }