@baig
here is what i was thinking... you'll have to add your logic to save and load the quiz.. and im not sure about how you want the user to interact with it but i think you get the idea.
class UserQuizlv extends Component
{
Public $quiz, $unansweredCount, $questions, $userAnswer, $currentQuestionId, $showResults;
public function Mount()
{
// my mockup for quiz data with the number of questions they requested.
// this is just my dummy data. obviously dont render all the answers like this to the browser.
$this->quiz = collect([
[ 'id' => 1,
'question' => 'this is question 1',
'answers' => [
['id' => 1, 'answer' => 'this is answer 1'],
['id' => 2, 'answer' => 'this is answer 2'],
['id' => 3, 'answer' => 'this is answer 3'],
],
'userAnswer' => null,
'correctAnswer' => 2,
],
[ 'id' => 2,
'question' => 'this is question 2',
'answers' => [
['id' => 1, 'answer' => 'this is answer 1'],
['id' => 2, 'answer' => 'this is answer 2'],
['id' => 3, 'answer' => 'this is answer 3'],
],
'userAnswerId' => null,
'correctAnswerId' => 2,
],
]);
$this->GetQuestion(); // prepare a question on first render.
}
public function getQuestion()
{
$this->unansweredCount = 1; // logic here to check/update the count of unanswered questions
$this->questions = $this->quiz->random(1); // logic here to get another random unanswered question
// set the new current question
$this->currentQuestionId = $this->questions->first()['id'];
}
public function nextQuestion()
{
// save answer to Quiz from variables...
$this->currentQuestionId;
$this->userAnswer;
$this->reset('userAnswer');
$this->getQuestion(); // load a new question.
}
public function showResults()
{
$this->questions = $this->quiz; // assuming all the answers are now saved there.
$this->showResults = true;
}
public function render()
{
return view('livewire.demo.user-quizlv');
}
and my demo blade...
<div>
@foreach($questions as $question)
<div class="grid grid-cols-1 gap-6">
<div class="block p-2 m-2 font-extrabold bg-green-200 text-green-400">
<p class="text-xl">{{ $question['question'] }} </p>
</div>
<div class="grid grid-cols-1 my-5 justify-center">
<div class="space-y-4 px-2">
@foreach($question['answers'] as $answer)
@if($showResults) {{-- show answer logic here . --}} @endif
<div class="flex items-start">
<div class="flex items-center h-5">
<input wire:click="$set('userAnswer', '{{ $answer['answer'] }}')" id="q_{{ $question['id'] }}_{{ $answer['id'] }}" name="q_{{ $question['id'] }}_{{ $answer['id'] }}" type="checkbox" class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded">
</div>
<div class="ml-3 text-sm">
<label for="q_{{ $question['id'] }}_{{ $answer['id'] }}" class="font-medium text-gray-700">{{ $answer['answer'] }}</label>
</div>
</div>
@endforeach
</div>
</div>
</div>
@endforeach
{{ $userAnswer }}
@if($unansweredCount > 1)
<div class="flex items-center justify-end mt-4">
<button wire:click="nextQuestion" type="submit" class="ml-4 bg-gray-400 p-2 rounded-md">Next</button>
</div>
@else
<div class="flex items-center justify-end mt-4">
<button wire:click="showResults" type="submit" class="ml-4 bg-gray-400 p-2 rounded-md">See Results</button>
</div>
@endif
</div>
it's not complete by any means but it puts the data where you want it. Let me know what you think.