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]),
]);