laravel-quiz icon indicating copy to clipboard operation
laravel-quiz copied to clipboard

$question->options returns null.

Open calebnanigah opened this issue 1 year ago • 0 comments
trafficstars

When I accessed the options values based on the question model, it returned null.

Here is the code snippet that accesses the options

`public function showQuizQuestions($quizId) { try { $quiz = Quiz::findOrFail($quizId); // Fetch questions associated with the quiz $questions = $quiz->questions; $questionsList = [];

        foreach ($questions as $question) {
            // Get the question details
            $questionDetails = Question::findOrFail($question->question_id);
            // Get the options for the question
            $options = $questionDetails->options; 

            // Build the question metadata array
            $questionMetadata = [
                'question_metadata' => $question,
                'question_text' => $questionDetails,
                'options' => $options,
            ];

            // Add the question metadata to the questions list
            $questionsList[] = $questionMetadata;
        }

        // Return the quiz details
        return response()->json(['message' => 'Quiz found with attached questions', 'data' => $questionsList], 200);

    } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
        // Return an error response if the quiz does not exist
        return response()->json(['error' => 'No topic quiz not found.'], 404);
    }
}`

As a workaround, I had to use: $options = QuestionOption::where('question_id', $questionDetails->id)->get();

calebnanigah avatar Mar 12 '24 04:03 calebnanigah