gwapo
gwapo

Reputation: 208

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

I'm trying to import an excel to my database table 'users' but it has an error saying Illegal string offset "email". I tried deleting the "email" then it says that Illegal string offset "username" now. Is it really the error in controller? Or maybe the reason is that i also have a repository. This is my code for the controller

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();
}

Upvotes: 0

Views: 900

Answers (1)

Wreigh
Wreigh

Reputation: 3287

As per your dumped $rows, it looks like that you don't need another foreach inside another foreach, modify your code.

// 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 already represents each row, so you should probably rename it to $row.

Upvotes: 1

Related Questions