I have gone through an earlier post Storing Array of Arrays Let me firstly show what I have tried
Code:
public function createQuestions(Request $request)
{
$elements = 0;
while($elements < count($data["question"])) {
$dataArray[] = array(
question' => $data['question'][$elements],
'answer' => $data['answer'][$elements],
'questionnaire_id' => $data['questionnaire_id'][$elements],
'type' => $data['type'][$elements],
'flag' => $data['flag'][$elements],
'choice' => $data['choice'][$elements],
'created_at' => Carbon\Carbon::now(), // only if your table has this column
'updated_at' => Carbon\Carbon::now(), // only if your table has this column
);
$elements++;
}
Question::create($dataArray);
return back()->with('message', 'Questionnaire is being created successfully');
}
The problems I am facing are different. Whenever I run the code. It throws an error
Undefined offset: 1
secondly it throws an error
General error: 1364 Field 'answer' doesn't have a default value. This error is being thrown for every database column.
I have declared these in fillable. Hence there should not be any issue.
protected $fillable = [
'question', 'idea_id', 'answer', 'type', 'choice', 'flag',
];
I have changed my app name to Questionnaire May be this is the issue?
fillableit should bequestionnaire_idnotidea_id. Thanks