lundi 3 septembre 2018

Importing to 'users' table but also imports to pivot tables

I am able to import for 'users' table but I also need to import to pivot tables. Here's my code from my UsersController for importing to 'users' table, but is it possible to supply data to pivot tables like "user_company", "user_department" by importing? Can you show me a sample of importing to pivot tables? Anyways this is my code in importing.

  if( Input::file('file_import') ) {
        $path = Input::file('file_import')->getRealPath();
        $inserts = [];
        Excel::load($path,function($reader) use (&$inserts)
        {
            foreach ($reader->toArray() as $row){
                $inserts[] = ['email' => $row['email'], 'username' => $row
                ['username'], 'password' => $row['password'], 'first_name' => $row['first_name'],'middle_name' => $row['middle_name'], 'last_name' => $row['last_name'], 'gender' => $row['gender'],
                'civil_status' => $row['civil_status'], 'spouse' => $row['spouse'], 'religion' => $row['religion'],'emergency_no' => $row['emergency_no'],'previous_work' => $row['previous_work'],
                'remarks' => $row['remarks'],'course' => $row['course'],'biometrics' => $row['biometrics'],'immediate_head' => $row['immediate_head'],'designation' => $row['designation'],'level' => $row['level'],
                'emp_status' => $row['emp_status'],'dependents' => $row['dependents'],'date_hired' => $row['date_hired'],'regularization_date' => $row['regularization_date'],'remmitance_date' => $row['remmitance_date'],
                'tin' => $row['tin'],'philhealth' => $row['philhealth'],'pagibig' => $row['pagibig'],'sss' => $row['sss'],'umid' => $row['umid'],'phone' => $row['phone'],'avatar' => $row['avatar'],
                'address' => $row['address'],'country_id' => $row['country_id'],'role_id' => $row['role_id'],'birthday' => $row['birthday'],'status' => $row['status']];
            }
        });
    }
  }

    if (!empty($inserts)) {
        DB::table('users')->insert($inserts);
        return back()->with('success','Inserted Record successfully');                  
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire