Reputation: 382
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?
Upvotes: 3
Views: 10421
Reputation: 111
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];
Upvotes: 7
Reputation: 1
Do Not get back to downgrade, you can easily use new updated version("maatwebsite/excel": "~3.1.0") with easiest import method.
Update Your Composer.json..
"require": {
**"maatwebsite/excel": "~3.1.0"**
},
php artisan make:import UsersImport
It will Create Import in app\imports\UserImport.php
<?php
namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UserImport implements ToCollection,WithHeadingRow
{
public function collection(Collection $rows)
{
return $rows;
}
// headingRow function is use for specific row heading in your xls file
public function headingRow(): int
{
return 3;
}
}
?>
In Your Controller file..
use Excel;
public function importExcel(Request $request) {
$import = new UsersImport();
$data = \Excel::import($import, request()->file('import_file'));
}
Upvotes: 0
Reputation: 8709
The first parameter of the import()
method is not the path to the file anymore in 3.1, but the class name of the Import file you have to create.
In this import file you can define how you want to read the sheet, and to what format you want to convert it, for example a model or a collection.
The easiest way to migrate from 2.1 to 3.1 is to create such an Import class by running (in your case):
php artisan make:import CsvDataImport
If you want to read all to rows at once, as in your question's code, you could use the ToCollection
Concern in your import file:
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class CsvDataImport implements ToCollection
{
public function collection(Collection $rows)
{
// Use the $rows collection to create your CsvData model.
}
}
In your Controller you can call the import like this:
use App\Imports\CsvDataImport;
use Maatwebsite\Excel\Excel;
Excel::import(new CsvDataImport, $path);
You can read more about importing to a collection here
Upvotes: 0