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

smartnathan's avatar

Making a Simple Quiz Application

Good morning, I am creating a simple quiz application where a user can add multiple choice questions and Select the right answer. I want to be able to get the correct answer and the associated option value. Please, how do I go about this. Or what is the best way to get this done. Thank you, I do appreciate.

View code

<div class="row">
        <div class="col-sm-12">
            <div class="input-group fg-float">
                <span class="input-group-addon"><i class="zmdi zmdi-pin-help

"></i></span>
                <div class="fg-line">
                    <input name="question" type="text" class="form-control">
                    <label class="fg-label">Question</label>
                </div>
            </div>
        </div>

    </div>
<br /><br />
    <div class="row">
                                <div class="col-sm-6">
                                    <div class="input-group fg-float">
                                        <span class="input-group-addon"><i class="zmdi zmdi-check-circle"></i></span>
                                        <div class="fg-line">
                                            <input name="name[]" type="text" class="form-control">
                                            <label class="fg-label">Optiion</label>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-sm-6">

                                            <div class="radio m-b-15">
                                <label>
                                    <input name="answer[]" type="radio" value="1">

                                    <i class="input-helper"></i>
                                    Option one is this and that-be sure to include why it's great
                                </label>
                                    </div>
                                </div>
                            </div>



<br /><br />
    <div class="row">
                                <div class="col-sm-6">
                                    <div class="input-group fg-float">
                                        <span class="input-group-addon"><i class="zmdi zmdi-check-circle"></i></span>
                                        <div class="fg-line">
                                            <input name="name[]" type="text" class="form-control">
                                            <label class="fg-label">Optiion</label>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-sm-6">

                                            <div class="radio m-b-15">
                                <label>
                                    <input name="answer[]" type="radio" value="1">
                                    <i class="input-helper"></i>
                                    Option one is this and that-be sure to include why it's great
                                </label>
                                    </div>
                                </div>
                            </div>



<br /><br />
    <div class="row">
                                <div class="col-sm-6">
                                    <div class="input-group fg-float">
                                        <span class="input-group-addon"><i class="zmdi zmdi-check-circle"></i></span>
                                        <div class="fg-line">
                                            <input name="name[]" type="text" class="form-control">
                                            <label class="fg-label">Optiion</label>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-sm-6">

                                            <div class="radio m-b-15">
                                <label>
                                    <input name="answer[]" type="radio" value="1">
                                    <i class="input-helper"></i>
                                    Option one is this and that-be sure to include why it's great
                                </label>
                                    </div>
                                </div>
                            </div>



<br /><br />
    <div class="row">
                                <div class="col-sm-6">
                                    <div class="input-group fg-float">
                                        <span class="input-group-addon"><i class="zmdi zmdi-check-circle"></i></span>
                                        <div class="fg-line">
                                            <input name="name[]" type="text" class="form-control">

                                            <label class="fg-label">Optiion</label>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-sm-6">

                                            <div class="radio m-b-15">
                                <label>
                                    <input name="answer[]" type="radio" value="1">
                                    <i class="input-helper"></i>
                                    Option one is this and that-be sure to include why it's great
                                </label>
                                    </div>
                                </div>
                            </div>

<button type="submit" class="btn btn-primary">Add Question</button>

Migration file for Quiz Options

Schema::create('quiz_options', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->softDeletes();
            $table->integer('quiz_question_id')->nullable();
            $table->string('name')->nullable();
            $table->boolean('is_answer')->nullable();
            $table->string('answer')->nullable();
            });

Migration file for Quiz Questions

 Schema::create('quiz_questions', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->softDeletes();
            $table->integer('quiz_type_id')->nullable();
            $table->string('question')->nullable();
            $table->boolean('is_active')->nullable();
            });
0 likes
3 replies
martinbean's avatar

@smartnathan What specifically is your question? If you want to get the option that’s the answer, just do a where condition:

$answer = $question->options()->where('is_answer', true)->first();
smartnathan's avatar

I am actually trying to create a view where an admin can add multiple choice questions and select the corresponding answers.

Please or to participate in this conversation.