K
K
kolyafat2016-03-24 06:36:46
Laravel
kolyafat, 2016-03-24 06:36:46

Collections and Bulk Populating a Table?

Good afternoon, kind and knowledgeable people, help me figure it out, I can’t insert a collection into an array in the database,

$collection = collect($name);
foreach($collection->chunk(1000) as $c_id_group){ // set the optimal value
Auto::insert($c_id_group->toArray());
usleep(100); // set pause value in microseconds
}

Columns are

id
c_id_group
name_group

In the $name array, the key is c_id_group, the value is name_group.
Tell me how to cope with these collections, I just can’t understand the logic, where you can read about it, in Russian documentation everything is too sparingly, as it seemed to me. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-03-24
@kolyafat

In general, the request is Auto::insert($c_id_group->toArray()); puts the values ​​​​of the keys of the array in the column names, so I got completely confused, how can I write the keys in one column and the values ​​\u200b\u200bin the other

$c_id_group->map(function($item, $key){
    return [
      'field1' => $key,
      'field2' => $item
      ];
})->toArray();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question