Home > Net >  Laravel : How to randomize the query of the Quiz with choices
Laravel : How to randomize the query of the Quiz with choices

Time:01-15

I'm planning to randomize the order of my query on quiz_question and choices but the quiz_question is the only one who gets randomized but the choices are still the same.

This is my code :

class QuestionByQuizID extends Controller
{
public function index(int $quiz_id){
    $questions = quizQuestions::where('quiz_id', '=', $quiz_id)->with(['choices'])->inRandomOrder()->get();
    if(is_null($questions)){
        return response()->json('Record not found!', 401);
    }
    return response(['message'=>"Questions displayed successfully", 
   'quizQuestions'=>$questions],200);
}

}

This is the sample query using postman

"message": "Questions displayed successfully",
"quizQuestions": [
    {
        "id": 580,
        "quiz_id": 36,
        "question_num": 10,
        "question_content": "What Are Those",
        "correct_answer": null,
        "created_by": null,
        "updated_by": null,
        "created_at": "2022-01-12T12:58:27.000000Z",
        "updated_at": "2022-01-12T12:58:27.000000Z",
        "question_image": null,
        "tagalog_question": "Ano yan!",
        "choices": [
            {
                "id": 1861,
                "quiz_questions_id": 580,
                "option": "choice 1",
                "remarks": 0,
                "created_at": "2022-01-12T12:58:27.000000Z",
                "updated_at": "2022-01-12T12:58:27.000000Z",
                "choices_image": null,
                "tagalog_choices": "tagalog"
            },
            {
                "id": 1862,
                "quiz_questions_id": 580,
                "option": "choice2",
                "remarks": 0,
                "created_at": "2022-01-12T12:58:27.000000Z",
                "updated_at": "2022-01-12T12:58:27.000000Z",
                "choices_image": null,
                "tagalog_choices": "tagalog"
            },
            {
                "id": 1863,
                "quiz_questions_id": 580,
                "option": "choice3",
                "remarks": 0,
                "created_at": "2022-01-12T12:58:27.000000Z",
                "updated_at": "2022-01-12T12:58:27.000000Z",
                "choices_image": null,
                "tagalog_choices": "tagalog"
            },
            {
                "id": 1864,
                "quiz_questions_id": 580,
                "option": "choice4",
                "remarks": 1,
                "created_at": "2022-01-12T12:58:27.000000Z",
                "updated_at": "2022-01-12T12:58:27.000000Z",
                "choices_image": null,
                "tagalog_choices": "tagalog"
            }
        ]

can anyone help me? thanks in advance.

CodePudding user response:

You can do additional query conditions for the choices relationship:

$questions = quizQuestions::where('quiz_id', '=', $quiz_id)->with(['choices' => function($query){
    $query->inRandomOrder();
}])->inRandomOrder()->get();

https://laravel.com/docs/8.x/eloquent-relationships#constraining-eager-loads

  •  Tags:  
  • Related