1

I am trying to store multiple records in my database but I'm getting errors. At the moment I have this:

$array=array('key'=>'value');
for($i = 0; $i < $something; $i++){
    array_add($array,'key','value')
}
DB::table('table')->insert($array);

Whenever I var_dump($array); It wont show the values of the array_add I add into the array.

What am I doing wrong and how should I insert like this?

15
  • Check if you exceed the maximum query size. Commented Mar 9, 2015 at 8:25
  • please edit your post and show your insert function too.. Commented Mar 9, 2015 at 8:34
  • @Yair.R My insert is right there... It's the: DB::table('table')->insert($array); This is in the Laravel framework query builder. Commented Mar 9, 2015 at 8:35
  • At least your array is mismatched, it starts array('key' => 'value'); and than in for loop it is array('key', 'value'); which are different... Commented Mar 9, 2015 at 8:39
  • @taliezin => and , do the same in this case? I've already tried to get both of them the same. Either => or , Commented Mar 9, 2015 at 8:44

3 Answers 3

1

What you are currently doing inside the for loop is setting key => value again and again. Instead you want to add an array with key => value to an array containing all the rows:

$rows = array(
    array('key' => 'value'),
    array('key' => 'value2'),
    // and so on...
);
for($i = 0; $i < $something; $i++){
    $rows[] = array('key' => 'value');
}
DB::table('table')->insert($rows);
Sign up to request clarification or add additional context in comments.

9 Comments

What if I had like 5 standard values. I just have to add a line of: $array[]=array('key2'=>'value2'); for every instance?
What exactly do you mean by standard values. 5 values that are the same for every row?
no like the first line you have atm. The first line of that code, already inserts a standard value. The for loop however, inserts different values the whole time depending on the $something(in my normal code) What if I wanted to add more values before the for loop like you did on the first line?
Then yes, just add some more lines. You can do it like you suggested in your first comment or like my edit shows.
I still have a problem with the outcome of the query though. I'm getting: Cannot add or update a child row: a foreign key constraint fails. I know this means that what I'm trying to insert in a foreign key, doesn't exist, but I am 100% sure that the value that's going to insert in the foreign key, does exist. The query it shows in the error is kinda messed up as well. It says the error is at: FOREIGN KEY (company_id) REFERENCES companies . The query it shows is: SQL: insert into table (company_id) values (3), ('value'), etc...
|
0

Here is an answer to this question. Hope this help. https://laracasts.com/discuss/channels/general-discussion/update-multiple-rows-in-a-foreach-loop?page=1#reply-27859

2 Comments

This is not an answer to my problem.
Hey @Loko, this is a multiple UPDATE, but you can use same approach to do multiple INSERT instead.
0

More a comment than an answer... Completely untested code...

Given my interpretation of the question - I think the code should look something like this....

If you wanted to add more than one record then an extra loop enclosing the 'newRecord' would be required.

Feel free to post comments if i misunderstood the question.

$allRecordsToInsert = array();

$newRecord = array();
for($i = 0; $i < $something; $i++) { // add columns to 'newRecord'
    array_add($newRecord, $curColKey, $curColValue);
}
// append new record to list of records to be inserted
$allRecordsToInsert[] = $newRecord;

DB::table('table')->insert($allRecordsToInsert);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.