简体   繁体   中英

Laravel Import CSV, index issue

Trying to figure out why I am getting the following error:

Undefined index Plugin ID

I am using Maatwebsite\\Excel for my import and tried using the guide here:

https://appdividend.com/2017/06/12/import-export-data-csv-excel-laravel-5-4/

I think I have everything in the right place, but I am getting the above error from this code:

public function import(Request $request)
{
if($request->file('imported-file'))
{
        $path = $request->file('imported-file')->getRealPath();
        $data = Excel::load($path, function($reader)
  {
        })->get();

  if(!empty($data) && $data->count())
  {
    foreach ($data->toArray() as $row)
    {
      if(!empty($row))
      {
        $dataArray[] =
        [
          'plugin_id' => $row['Plugin ID'],
          'cve' => $row['CVE'],
          'cvss' => $row['CVSS'],
          'risk' => $row['Risk'],
          'host' => $row['Host'],
          'protocol' => $row['Protocol'],
          'port' => $row['Port'],
          'name' => $row['Name'],
          'synopsis' => $row['Synopsis'],
          'description' => $row['Description'],
          'solution' => $row['Solution'],
          'see_also' => $row['See Also'],
          'plugino_utput' => $row['Plugin Output']
        ];
      }
  }
  if(!empty($dataArray))
  {
     Shipping::insert($dataArray);
     return back();
   }
 }
}
}

This is in my controller file and is trying to account for the headers being different in the CSV compared to in my database.

Any idea why it would be complaining about index on a column from the csv side of things?

I ended up with this for now from another post. The post still had an extra section in it, but doing a var_dump on $value (which I left in, but commented out) I could see that $value was already an array, so instead of passing it into another array, I tried just inserting with it and that seems to be working.

Still working on placing the error and success messages.

Thanks to ljubadr for helping me learning how to put some print type statements in with the code to see what was getting output at various places.

public function importExcel(Request $request)
{
    if($request->hasFile('import_file')){
        $path = $request->file('import_file')->getRealPath();
        $data = Excel::load($path, function($reader) {})->get();
        if(!empty($data) && $data->count()){
            foreach ($data->toArray() as $key => $value) {
                if(!empty($value)){
                    #var_dump($value);
                    Item::insert($value);
                }
            }
        return back()->with('success','Insert Record successfully.');
        }
    }
#return back()->with('error','Please Check your file, Something is wrong there.');
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM