input('limit', 10))); $query = Question::query(); 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); } $questions = $query->inRandomOrder()->limit($limit)->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; } }