harishdurga / laravel-quiz

With this package, you can easily get quiz functionality into your Laravel project.
MIT License
162 stars 19 forks source link

$question->options returns null. #45

Open calebnanigah opened 8 months ago

calebnanigah commented 8 months ago

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();