导入 excel 在我的控制器中出现 "Illegal string offset" 错误

Importing excel has an error of "Illegal string offset" in my controller

我正在尝试将 excel 导入我的数据库 table 'users',但它有一个错误提示非法字符串偏移量 "email"。我尝试删除 "email" 然后它说 Illegal string offset "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();
}

根据你转储的 $rows,看起来你不需要另一个 foreach 里面的另一个 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']];
}

$rows 已经代表 each 行,因此您可能应该将其重命名为 $row.