| 123456789101112131415161718192021222324252627282930313233 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use App\Models\Question;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- class QuestionRandomController extends Controller
- {
- public function __invoke(Request $request): JsonResponse
- {
- $limit = max(1, min(50, (int) $request->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);
- }
- $questions = $query->inRandomOrder()->limit($limit)->get();
- return response()->json([
- 'count' => $questions->count(),
- 'data' => $questions,
- ]);
- }
- }
|