I'm creating an online test portal.
The table structure is
This is working perfectly. I just want to know is there any relationship we can establish so that we can perform
$question->user->answer
or, something like,
$user->question->answer
To easily find that whether a user has given the correct answer for a particular question or not.
Each question will have only one entry in live_test_answers for each user.
You need to go through your live_test_answers
table in order to get the answer and the question.
// For the example let's fetch the first answer the user gave
$answer = $user->answers()->first();
// Now let's fetch the question related to the answer
$question = $answer->question;
// Now you can check the answer for the question, something like
$isCorrect = $answer->answer == $question->answer;