代码之家  ›  专栏  ›  技术社区  ›  gwapo

导入Excel在我的控制器中有“非法字符串偏移量”错误

  •  0
  • gwapo  · 技术社区  · 6 年前

    我正试图将Excel导入数据库表“users”,但它有一个错误,即“email”是非法的字符串偏移量。我尝试删除“email”,然后它说现在非法的字符串偏移“username”。这真的是控制器的错误吗?或者原因是我也有一个存储库。 这是我的控制器代码

    public function userImport()
    {
        if( Input::file('file_import') ) {
            $path = Input::file('file_import')->getRealPath();
            $inserts = [];
            Excel::load($path,function($reader) use (&$inserts)
            {
            foreach ($reader->toArray() as $rows){
                foreach($rows 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');                  
        }
    
        return back();
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Wreigh    6 年前

    按照你甩的 $rows 看起来你不需要另一个 foreach 在另一个里面 前额 ,修改代码。

    // readability purpose
    $rows = $reader->toArray();
    
    foreach ($rows 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']];
    }
    

    行数 已经表示 每个 行,因此您可能应该将其重命名为 $row .