Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$question->options returns null. #45

Open
calebnanigah opened this issue Mar 12, 2024 · 0 comments
Open

$question->options returns null. #45

calebnanigah opened this issue Mar 12, 2024 · 0 comments

Comments

@calebnanigah
Copy link

calebnanigah commented Mar 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant