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

magwag's avatar

Building quiz app in Laravel - A little conceptual help appreciated

Hi everyone,

I'm very new to Laravel, and application building in general, but I'm trying to build a quiz application.

It will be purely multiple choice, with four answers per question. You'll be able to select a quiz, complete it, and then get your score.

I've set up my migrations like so:

Quizzes

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

Questions

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

Answers

    public function up()
    {
        Schema::create('answers', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('question_id')->unsigned()->index();
            $table->string('answer');
            $table->boolean('is_correct');
            $table->timestamps();
        });
    }

and my models like so:

Quiz

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

Question

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

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

Answer

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

I'd like the user to have one question to answer on screen at any one time, so I've set up the following view for this:

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div id="quiz-wrapper">
                    <h1>{{ $question->body }}</h1>
                    {!! Form::open() !!}
                    @foreach($question->answer->shuffle() as $answer)
                    <h3>
                    <div class="form-group">
                        <div class="radio">
                            {{Form::radio('result', $answer->is_correct) }}
                            {{ Form::label('answer', $answer->answer) }}
                        </div>
                    </div>
                    </h3>
                    @endforeach
                    {{Form::submit('Submit answer') }}
                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>
@endsection

I want to store the user's answer to each question as they go along, but am drawing a bit of a blank as to where to go next. I think I need a users table where I store the results of each question on submission, but can't quite conceptualise what this would look like.

Any help would be much appreciated.

0 likes
2 replies
lmxdev's avatar

So far what you did makes sense

public function answers() {     //forgot the 's'
        return $this->hasMany(Answer::class);
    }

also you should pass the quizz object to your view so that you can do:

(i don't know how you select the current question, at random?)

<div id="quiz-wrapper">
    <h1>{{ $quizz->question->body }}</h1>
        {!! Form::open() !!}
            @foreach($quizz->question->answer->shuffle() as $answer)
                <h3>
                    <div class="form-group">
                        <div class="radio">
                            {{Form::radio('result', $answer->is_correct) }}
                            {{ Form::label('answer', $answer->answer) }}
                        </div>
                    </div>
                </h3>
            @endforeach
        {{Form::submit('Submit answer') }}
     {!! Form::close() !!}
 </div>

Please or to participate in this conversation.