1

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

enter image description here

2
  • try this; Question::create($request->all()); Commented Nov 20, 2016 at 12:26
  • i think create does not store bulk data? Commented Nov 20, 2016 at 12:32

1 Answer 1

2

If you want to use insert() for bulk insertion you should prepare data for it. Structure should be an array of arrays, like in this example:

$data = [
            ['name' => 'Afghanistan', 'code' => 'AFG'],
            ['name' => 'Albania', 'code' => 'ALB'],
            ['name' => 'Algeria', 'code' => 'DZA'],
        ];
Sign up to request clarification or add additional context in comments.

7 Comments

I know this but i am confused. I have fields in db as question, answer now what would i give instead of you have given? $data = [ '??' => '??']; ok for field i can give $data = [ 'question' => '??']; but what about the other? i dont know what is going to be here? thanks
@QasimAli I don't know what exactly data structure you're using, but it looks like your data should look like this ['answer' => 'Seventeen ducks', 'question' => 'How many ducks?', 'questionnaire_id' => 15]
yes, the data should look like this, but please bear with me if i am irritating. as of now I am pretty confused. Suppose i have added how many ducks ? in input field? How would i tell to laravel to grab that from its every index? question => What to give here? to grab the questions of input field?
@QasimAli I can't see how are you getting this data. Please show your form and result of dd($data). I guess user can add multiple similar questions and answers per each request?
I have edited the question. Please have a look at snapshot and html
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.