vendredi 24 mars 2017

Splitting one table into two tables

I have a temp table that has 5000 records. I'm trying to split it into two tables that are joined by id. I'm using eloquent to take all the rows from the temp table and do a foreach to insert some of the data into table 1 then get last inserted row id and insert the other data into the table 2 with the id. After waiting for about 5 mins I check the tables and I see duplicates. All rows get copied twice. Both tables have 10000 records instead of 5000. Any ideas?

Here is my code:

$groups = \Illuminate\Support\Facades\DB::table('temp_table')->get();

foreach($groups as $group){
  $user = App\Users\User::create([
  'first_name'  => $group->first_name,
  'last_name'  => $group->last_name,
  ....
  )];

  $role = App\Roles\Role::create([
  'user_id'  = $user->id,
  'name' = $group->name,
  .... 
  ]);
}

Thank you



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2neRGhf
via IFTTT

Aucun commentaire:

Enregistrer un commentaire