Mustaque Ahmed
Mustaque Ahmed

Reputation: 135

Laravel: Show multiple rows one by one under same id in a table

I have a table which has multiple rows under same id (quiz_id). I'm trying to show all of those rows one-by-one by clicking next button in the view file. In the picture below there are 2 quiz ids 1 and 2. Every Id has 3 questions. When a user first goes to my view page with quiz_id 1 they may see the 1st question and answer this. After, the user clicks next button and can answer 2nd question. Finally the 3rd.

Can anyone please tell me how to do this in Laravel 5.5? I am a newbie in Laravel and sorry for poor English.

enter image description here

Upvotes: 0

Views: 3184

Answers (1)

Lewis Johnson
Lewis Johnson

Reputation: 340

With the question, you asked it's hard to answer with such little detail.

I'm going to assume you have a controller and model "Question". You can use a for-loop in your blade file to achieve what you want.

class QuestionController extends Controller{
    public function showbyQuizId($id){
        $questions = Question::where('quiz_id', $id)->get();
        return view('question')->with('questions', $questions);
    }
}

And for your blade file question.blade.php

@foreach($questions as $question)
    <h1>{{$question->question}} ?</h1>
    <p>Pick from these 4 options</p>

    <button>{{$question->choice1}}</button>
    <button>{{$question->choice2}}</button>
    <button>{{$question->choice3}}</button>
    <button>{{$question->choice4}}</button>
@endforeach

From there you can set up a form or javascript to test if the selected button was correct with the server, and then redirect to the next question.

If you need any more help, let me know.

Upvotes: 2

Related Questions