I know there are other questions for bulk insertion in Laravel. But I am facing an issue when I try to store it in the database.
I am using insert() for bulk insertion. I have an array of data coming through request. I have never used insert method before. it is throwing an error:
Array to string conversion (SQL: insert into
questions(answer,question,questionnaire_id) values (1995, What is model of your bike1, ?))
I have developed a questionnaire. Questions are being added dynamically on button click using jQuery.
Here is the method I am trying to store data:
/**
* Create questions
*
*/
public function createQuestions(Request $request)
{
// Insert new records
$data[] = $request->except('_token');
Question::insert($data);
return back()->with('message', 'Questionnaire is being created successfully');
}
EDIT:
This is the html code which is being appended by jquery and user can add multiple question
'<div class="question'">
<div class="form-group">'+
<label class="control-label col-md-4" for="question-type">Question Type:</label>
<div class="col-md-4">'+
<select class="form-control question-type" id="'+counter+'">
<option value="text" id='+counter+'>Text</option>
<option value="multiple" id="'+counter+'">Multiple Choice</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" for="question">Enter Question:</label>
<div class="col-md-4">
<input type="text" name="question[]" class="form-control" />'+
</div>
<div class="col-md-4">
<button type="button" class="btn btn-danger btn-delete " id="'+counter+'">Delete Question</button>
</div>
</div>
<div class="form-group txt-answer'+counter+'">
<label class="control-label col-md-4" for="answer">Answer:</label>
<div class="col-md-4">
'<input type="text" name="answer[]" id="answer" class="form-control" />
</div>
</div>
<hr>
<div>
</div>
Here is dd result

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