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

BikashKatwal's avatar

Creating a POST API to Save Many relational data

 $question = new Question();
        $question->question = $request->question;
        $question->question_image_url = $request->question_image_url;
        $question->category_id = $request->category_id;
        $question->question_type = $request->question_type;
        $question->save();
        $question->answers()->saveMany([
            new Answer(['answer' => "Kancha", "is_correct" => false]),
            new Answer(['answer' => "Kanchi", "is_correct" => true]),
            new Answer(['answer' => "Kanchi", "is_correct" => false]),
            new Answer(['answer' => "Kanchi", "is_correct" => false]),
        ]);

        return response([
            'data' => new QuestionResource($question)
        ]);

In the code above, saving many answer is manually done, but I have to create a POST API to saveMany Answer. Could you please suggest me how to create POST Api in this case and get the request in controller.

0 likes
7 replies
BikashKatwal's avatar

In addition,

public function store(QuestionRequest $request)
    {
        return $request->all();

//        foreach ($request->all('option1') as $opt){
//           return $opt->answer;    //Not working
//        }
        
        
//        $question = new Question();
//        $question->question = $request->question;
//        $question->question_image_url = $request->question_image_url;
//        $question->category_id = $request->category_id;
//        $question->question_type = $request->question_type;
//        $question->save();
//        $question->answers()->saveMany([
//            new Answer(['answer' => "Kancha", "is_correct" => false]),
//            new Answer(['answer' => "Kanchi", "is_correct" => true]),
//            new Answer(['answer' => "Kanchi", "is_correct" => false]),
//            new Answer(['answer' => "Kanchi", "is_correct" => false]),
//        ]);
//
//        return response([
//            'data' => new QuestionResource($question)
//        ]);
    }

-----------------------request-------------------------

{
    "question": "What is your horse name?",
    "question_image_url": null,
    "category_id": "1",
    "question_type": "2",
    "option1": "['answer': \"Kancha\", \"is_correct\" : false]",
    "option2": "['answer' : \"Kancha\", \"is_correct\": false]",
    "option3": "['answer' : \"Kancha\", \"is_correct\" : true]",
    "option4": "['answer': \"Kancha\", \"is_correct\" : false]"
}

This is a request returned while posting the data, and this is how I am planning to do, but I am not sure of the correct process to follow in this scenario.

I am trying to get answer and is_correct from the request and using below saveMany want to store in the Answer table.

        $question->answers()->saveMany([
            new Answer(['answer' => "Kancha", "is_correct" => false]),
            new Answer(['answer' => "Kanchi", "is_correct" => true]),
            new Answer(['answer' => "Kanchi", "is_correct" => false]),
            new Answer(['answer' => "Kanchi", "is_correct" => false]),
        ]);
Snapey's avatar

Just insert the data?

        $question->answers()->saveMany([
            new Answer(['answer' => $request->option1['answer'], "is_correct" => $request->option1['is_correct']]),
            new Answer(['answer' => $request->option2['answer'], "is_correct" => $request->option2['is_correct']]),
            new Answer(['answer' => $request->option3['answer'], "is_correct" => $request->option3['is_correct']]),
            new Answer(['answer' => $request->option4['answer'], "is_correct" => $request->option4['is_correct']]),
        ]);

BikashKatwal's avatar

Thanks @snapey, but I am getting "message": "Illegal string offset 'answer' ", error.

public function store(QuestionRequest $request)
    {

        return $request->option1['answer'];

        $question = new Question();
        $question->question = $request->question;
        $question->question_image_url = $request->question_image_url;
        $question->category_id = $request->category_id;
        $question->question_type = $request->question_type;
        $question->save();
        $question->answers()->saveMany([
            new Answer(['answer' => $request->option1['answer'], "is_correct" => $request->option1['is_correct']]),
            new Answer(['answer' => $request->option2['answer'], "is_correct" => $request->option2['is_correct']]),
            new Answer(['answer' => $request->option3['answer'], "is_correct" => $request->option3['is_correct']]),
            new Answer(['answer' => $request->option4['answer'], "is_correct" => $request->option4['is_correct']]),
        ]);

        return response([
            'data' => new QuestionResource($question)
        ]);
    }
Snapey's avatar

not sure how you are sending the data, but each option just appears to be a string? You need to decode it or send it in a more appropriate format

JABirchall's avatar

Your receiveing the data as a json array. you need to decode your request first.

json_decode($request->input('option1'))['answer']

BikashKatwal's avatar

My raw data to POST

{
    "question":"What is you Mobile?",
    "question_image_url":"",
     "category_id":2,
    "question_type":1,
    "option1":{"answer": "Apple", "is_correct": 1},
    "option2":{"answer": "Samsung", "is_correct": 0},
    "option3":{"answer": "Oppo", "is_correct": 0},
    "option4":{"answer": "Huwaii", "is_correct": 0}
}

and I followed the below code to save the data

 $question = new Question();
        $question->question = $request->question;
        $question->question_image_url = $request->question_image_url;
        $question->category_id = $request->category_id;
        $question->question_type = $request->question_type;
        $question->save();
        $question->answers()->saveMany([
            new Answer(['answer' => $request->input('option1')['answer'], "is_correct" => $request->input('option1')['is_correct']]),
            new Answer(['answer' => $request->input('option2')['answer'], "is_correct" => $request->input('option2')['is_correct']]),
            new Answer(['answer' => $request->input('option3')['answer'], "is_correct" => $request->input('option3')['is_correct']]),
            new Answer(['answer' => $request->input('option4')['answer'], "is_correct" => $request->input('option4')['is_correct']]),
        ]);

        return response([
            'data' => new QuestionResource($question)
        ]);

and my get request returns:

{
    "data": [
        {
            "question": "What is you laptop name?",
            "answer": "Dell",
            "url": null,
            "category": 2,
            "type": 1,
            "options": [
                "Apple",
                "Gigabyte",
                "Dell",
                "Lenevo"
            ]
        },
        {
            "question": "What is you Mobile?",
            "answer": "Apple",
            "url": null,
            "category": 2,
            "type": 1,
            "options": [
                "Huwaii",
                "Oppo",
                "Samsung",
                "Apple"
            ]
        }
    ]
}

Is the GET request correc?

Did I follow the correct way or is there still a place for improvement. What can be done for reducing the line of code?

Thank you for providing an idea.

Snapey's avatar
Snapey
Best Answer
Level 122

As I suggested before. You can only have one correct answer. Create an input field for 'correct' and then create multiple 'alternate' answers. These could be an array.

Then you don't need to keep handling the correct answer flag. You just set it on the answers in the database for the correct answer.

With data;

{
    "question":"What is you Mobile?",
    "question_image_url":"",
    "category_id":2,
    "question_type":1,
    "correct":"Apple",
    "options":["Samsung", "Oppo","Huwaii"]
}

then

    $question = new Question();
    $question->question = $request->question;
    $question->question_image_url = $request->question_image_url;
    $question->category_id = $request->category_id;
    $question->question_type = $request->question_type;
    $question->save();

    $question->answers()->create(['answer' => $request->correct, 'is_correct' => true]),

    foreach($request->options as $option) {
        $question->answers()->create(['answer' => $option]);
    }

    return response([
        'data' => new QuestionResource($question)
    ]);

Please or to participate in this conversation.