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

asdasdsa's avatar

Insert into two (2) tables

Hi!

Background info: I'm trying to build a small quiz app. Where admin and mods can compose questions under different subjects, with four (4) options, one of them is correct. And then users should be able to participate in a quiz (but thats will be my next thread after this).

So far, I have two (2) tables answers and questions.

First off, answers:

        Schema::create('answers', function (Blueprint $table) {
            $table->increments('id');
            $table->text('content');
            $table->boolean('correct');
            $table->integer('question_id');
            $table->timestamps();
        });

Then questions:

        Schema::create('questions', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('subject');
            $table->text('question');
            $table->text('question_source');
            $table->string('image_url')->nullable();
            $table->timestamps();
        });

I have a QuestionsController where i save the question at this point, but how should i do to save the answers as well?

    public function store(Request $request, Question $question){

        $this->validate($request, [
            'subject' => 'required',
            'question' => 'required|min:10',
            'question_source' => 'required|min:10',
            'answer.0' => 'required',
            'answer.1' => 'required',
            'answer.2' => 'required',
            'answer.3' => 'required',
            'correct' => 'required|in:1,2,3,4',
            'file' => 'mimes:jpeg,jpg,png']);

        $question = new Question;

        $question = Question::create($request->all());

        $request->session()->flash('message', 'Question was successfully added!');
        return back();
    }
}

So have can i save values from one form, into two tables?

0 likes
3 replies
davorminchorov's avatar

I wrote something but I totally missed the point of the question.

There are 2 (or maybe more) ways to deal with foreign keys.

One of them is to set it manually.

$answer = Answer::create([
    'question_id' => $question->id // or whatever you get the ID, maybe through a URL parameter.
]);

The other way is to save the ID through the relationship like:

$answer = Answer::question()->save($question); // this will automatically fill the question ID in the answers table.

but my question for you is: What are your relationships between the tables? Is it one to many or many to many?

I am asking, because there are questions which can have one answer (one choice) or multiple answers depending on the type of the question. (multiple choice)

1 like
asdasdsa's avatar

Questions have four (4) choice. But only one will be correct. So oneToMany relation?

davorminchorov's avatar

This is how I would do it:

From the form, you probably store the question ID somewhere in the view (maybe passed as a URI parameter, /admin/questions/{$id}/answer or something similar) so when you hit the URI with a post request, in your Admin/AnswerController, you'll have the following data:

Question ID
Answer Content to store

so what you do is this in your store correct answer method:

public function store(Request $request, $questionId) {
    // validate the request / use form request for validation   

    $question = Question::findOrFail($questionId); // this will find the question your admin sets a correct answer for.
    // findOrFail returns ModelNotFoundException if the result returned is null / the question with that ID is not found
    
    $answer = Answer::create([
        'question_id' => $question->id, // Don't forget to set the fillable fields in the Answer model, and include question_id with them
        'content' => $request->answer_content,
        'correct' => true
    ]);

    // Flash message

    // return view after submit
}

but if you wanna do it in one form where you put the questions and the answer, you can do it this way:


// form validation
 
$question = Question::create($request->except('answer_content'));

$answer = Answer::create([
        'question_id' => $question->id, // Don't forget to set the fillable fields in the Answer model, and include question_id with them
        'content' => $request->answer_content,
        'correct' => true
]);

// flash message

//return view / redirect

Please or to participate in this conversation.