Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mezie's avatar
Level 8

How to go about this?

Please suggestions on how to go about this will be really appreciated. Am building a quiz app, where users can selected subject and based on the subject selected set of questions (one question per page) are displayed and upon completion and submit the user is graded based on the number of questions right and wrong.

The questions are coming from a database table with 4 possible options and also the correct option is store in the database .

My challenge now is displaying the question and allowing for next and previous questions.

I tried using Laravel pagination but it not working because am using POST data to populate the query which the pagination is using, so it not working when the next button is clicked.

0 likes
13 replies
SaeedPrez's avatar

You need to have two controllers, one that handles the question displaying and one that handles that answer. Once the user submits an answer, the answer gets recorded in session or database, and the controller redirects to a new (random?) question until X number of questions are answered. You can then use a third controller to show the score display.

mezie's avatar
Level 8

@SaeedPrez thanks. I get that. But where am currently having challenge is paginating the questions.

SaeedPrez's avatar

@mezie are you trying to use Laravel's pagination? If so, you could do something like this..

public function getAnswer(Request $request)
{
    // Get & save the answer

    $nextPage = $request->input('page') + 1;

    return redirect()->route('question.show', ['page' => $nextPage]);
}
mezie's avatar
Level 8

@SaeedPrez I currently have this in my controller...

`` public function getQuetions($subj_code) { $subject = Subject::where('subj_code', $subj_code) ->where('level_id', Auth::user()->level_id) ->firstOrFail();

    $questions = Question::where('subject_id', $subject->id)->paginate(8);

    return view('questions', compact('questions'));        
}

``

SaeedPrez's avatar

@mezie if youhave ->paginate(8) that means you want 8 questions per page.. I thought you wanted 1 question per page?

SaeedPrez's avatar

@mezie where do you post the questions? You need to also have a hidden input field named page with the current page id in your form, so you can keep track of the current page and send the user to the next page when they submit an answer. Like I showed earlier.

mezie's avatar
Level 8

@SaeedPrez This is my codes so far:

Route::post('/questions/getQuestions', 'QuestionsController@getQuestions')

My view:

``

{{ csrf_field() }} @foreach ($subjects as $subject) {{ $subject->name }} @endforeach
     <button type="submit" class="btn btn-primary">Start Test</button>
``

My controller:

`` public function getQuestions(Request $request) { $questions = Question::where('subject_id', $request->input('subject')->paginate(1);

    return view('questions', compact('questions'));

} ``

SaeedPrez's avatar

Ok, it seems you've just started and I've already explained to you what the steps are, so just keep building on that.. It's not that complicated, give it a shot :)

SaeedPrez's avatar
  1. You need a show question controller method with ->paginate(1) to get one question at the time, you also need to send the current page number to the view.
  2. You need to add <input type="hidden" name="page" value="{{ $page }}"> to your view form to keep track of the current page
  3. You need a submit answer controller method to handle the answer, like save it in session or database and redirect to next page
  4. Once all questions are answered, your submit answer controller should redirect to a score page where you show the user how many questions they answered correctly

Makes sense?

mezie's avatar
Level 8

Make sense, but what about to handle previous question

SaeedPrez's avatar

@mezie

You need to figure out what your needs are and how you want your application to work, and then decide what works best to accomplish that.

You could for example save all questions/answers in the session and when everything is done save everything to the database all at once or save each question/answer to the database after each answer.

Please or to participate in this conversation.