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

david001's avatar

Online exam quiz app help needed

Hi, i am making a quiz application with Laravel. I am stuck here.

my main question/problem

  1. How to randomly fetch one question with four answer per page . I have 50 questions with answer in table
  2. Among 50 question i want to allow user to attempt any 10 no-repeatable questions
  3. Each question should have previous and next button. on 10th question it should show submit button
  4. Only logged in users attempt the question.

will you please give me some idea about solving these 4 problem. I have main difficulty in question no.1 , fetching one question at a time and showing next button

users table

//....

quizees table

    public function up()
    {
        Schema::create('quizzes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }

Answers table




public function up()
    {
        Schema::create('questions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('quiz_id')->unsigned()->index();
            $table->string('body');
            $table->timestamps();
        });
    }

questions table




    public function up()
    {
        Schema::create('questions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('quiz_id')->unsigned()->index();
            $table->string('body');
            $table->timestamps();
        });
    }

Answer.php


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Question;
class Answer extends Model
{
    public function question() {
        return $this->belongsTo(Question::class);
    }
}

Question.php


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Quiz;
use App\Answer;

class Question extends Model
{
    public function quiz() {
        return $this->belongsTo(Quiz::class);
    }

    public function answers() {
        return $this->hasMany(Answer::class);
    }
}

method


    public function quiz()
    {
        $question = Question::with('answers')->get();
        return $question;
        return view('home',compact('question'));
    }

output around 50 questions


[
{
id: 1,
quiz_id: 1,
body: "what is 2+2?",
created_at: null,
updated_at: null,
answers: [
{
id: 1,
question_id: 1,
answer: "4",
is_correct: 1,
created_at: null,
updated_at: null
},
{
id: 2,
question_id: 1,
answer: "2",
is_correct: 0,
created_at: null,
updated_at: null
},
{
id: 3,
question_id: 1,
answer: "3",
is_correct: 0,
created_at: null,
updated_at: null
},
{
id: 4,
question_id: 1,
answer: "1",
is_correct: 0,
created_at: null,
updated_at: null
}
]
},
{
id: 2,
quiz_id: 1,
body: "what is square of 3",
created_at: null,
updated_at: null,
answers: [
{
id: 9,
question_id: 2,
answer: "3",
is_correct: 0,
created_at: null,
updated_at: null
},
{
id: 10,
question_id: 2,
answer: "9",
is_correct: 0,
created_at: null,
updated_at: null
},
{
id: 11,
question_id: 2,
answer: "4",
is_correct: 0,
created_at: null,
updated_at: null
},
{
id: 12,
question_id: 2,
answer: "6",
is_correct: 0,
created_at: null,
updated_at: null
}
]
},
{
id: 3,
quiz_id: 1,
body: "what is colour of blood",
created_at: null,
updated_at: null,
answers: [
{
id: 5,
question_id: 3,
answer: "yellow",
is_correct: 0,
created_at: null,
updated_at: null
},
{
id: 6,
question_id: 3,
answer: "red",
is_correct: 0,
created_at: null,
updated_at: null
},
{
id: 7,
question_id: 3,
answer: "blue",
is_correct: 0,
created_at: null,
updated_at: null
},
{
id: 8,
question_id: 3,
answer: "pink",
is_correct: 0,
created_at: null,
updated_at: null
}
]
}
]

Any help would be greatly appreciated

0 likes
0 replies

Please or to participate in this conversation.