I've upgraded Maatwebsite/Laravel-Excel to 3.1 from 2.1. Some of the method are deprecated.
Here is the code on version 2.1.
$data = Excel::load($path, function($reader) {})->get()->toArray();
It's working when I used 2.1 but after I upgraded, it's error
Call to undefined method Maatwebsite\Excel\Excel::load()
For version 3.1, i tried to change to
$data = Excel::import($path, function($reader) {})->get()->toArray();
I know this probably not the correct syntax.
Here my full code when developing using 2.1.
$path = $request->file('csv_file')->getRealPath();
if ($request->has('header')) {
$data = Excel::import($path, function($reader) {})->get()->toArray();
} else {
$data = array_map('str_getcsv', file($path));
}
if (count($data) > 0) {
if ($request->has('header')) {
$csv_header_fields = [];
foreach ($data[0] as $key => $value) {
$csv_header_fields[] = $key;
}
}
$csv_data = array_slice($data, 0, 8);
$csv_data_file = CsvData::create([
'csv_filename' => $request->file('csv_file')->getClientOriginalName(),
'csv_header' => $request->has('header'),
'csv_data' => json_encode($data)
]);
} else {
return redirect()->back();
}
return view('import.import_field', compact( 'csv_header_fields', 'csv_data', 'csv_data_file'));
How to fix this error on version 3.1?
Actually there is no need to create any extra classes for the Excel import in Maatwebsite/Laravel-Excel version 3. Basically you can do the whole CSV to array conversion almost in a same way as in version 2:
$path = $request->file('csv_file')->getRealPath();
$data = \Excel::toArray('', $path, null, \Maatwebsite\Excel\Excel::TSV)[0];