I'm new to the blog and I joined because I have a problem for a while.
The following code is for importing users through a .csv file into a database, but when I try to import users, the following error appears.
8
"Undefined index: name"
"D:\xampp\htdocs\plataforma\app\Http\Controllers\UserController.php"
39
array:7 [▼
  "request" => Request {#43 ▶}
  "validator" => Validator {#257}
  "file" => UploadedFile {#247 ▶}
  "csvData" => """
    name,username,email,password,role,idSede,idManager\r\n
    Claudio Magallan,claudiomg,claudiomagallan@hotmail.com,secret,2,60,0\r\n
    Cristina Vargas,cristyvg,cristyvargas@institutodeventas.com,secret,3,61,4\r\n
    """
  "rows" => array:3 [▶]
  "header" => array:7 [▶]
  "row" => array:7 [▶]
]
I really appreciate your help and I hope you can help with my problem. I attach my controller code.
public function upload(Request $request) {
  $validator = Validator::make($request->all(), [
    'file' => 'required'
  ]);
  if ($validator->fails()) {
    return redirect()
    ->back()
    ->withErrors($validator);
  }
  $file = $request->file('file');
  $csvData = file_get_contents($file);
  $rows = array_map("str_getcsv", explode("\n", $csvData));
  $header = array_shift($rows);
  foreach ($rows as $row) {
    $row = array_combine($header, $row);
    User::create([
      'name' => $row['name'],
      'username' => $row['username'],
      'email'    => $row['email'],
      'password' => bcrypt($row['password']),
      'role'     => $row['role'],
      'idSede'   => $row['idSede'],
      'idManager'=> $row['idManager'],
    ]);
  }
  flash('Users imported');
  return redirect()->back();
}
The error mark the next line code:
'name' => $row['name'],
Regards!
 
    