Too Few Arguments To Function Maatwebsite\Excel\Excel::import(), 1 Passed And At Least 2 Expected
I am trying to Import Excel Data in Laravel and Insert into Database. I am using maatwebsite/excel version 3 composer package and laravel version 5.8
Error screenshot:https://imgur.com/a/2KXCE0g
Blade file: import.blade.php
<form action="{{ url('/import-excel/import') }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="importFile">Select Import File</label>
<input type="file" name="select_file" class="form-controll">
</div>
<input type="submit" name="upload" class="btn btn-success" value="Import File">
</form>
Controller file: ImportExcelController.php
public function import(Request $request){
$this->validate($request, [
'select_file' => 'required|mimes:xls,xlsx'
]);
$path = $request->file('select_file')->getRealPath();
$data = Excel::import($path)->get();
if($data->count() > 0){
foreach($data->toArray() as $key => $value){
foreach($value as $row){
$insert_data[] = array(
'CustomerName' => $row['customer_name'],
'Gender' => $row['gender'],
'Address' => $row['address'],
'City' => $row['city'],
'PostalCode' => $row['postal_code'],
'Country' => $row['country'],
);
}
}
if(!empty($insert_data)){
DB::table('tbl_customer')->inset($insert_data);
}
}
return back()->with('success', 'Import data successfully!');
}
I have checked excel.php file are exits in config folder. provider and aliases added.
route
Route::get('/import-excel', '[email protected]');
Route::post('/import-excel/import', '[email protected]');
how to solve this please?
Answer
This is the method signature for the import method:
public function import($import, $filePath, string $disk = null, string $readerType = null);
Which means that the path is a second parameter, but you are missing the first one.
The first one should be an import
class, you create one like this as an example
php artisan make:import UsersImport --model=User
Then to use it:
Excel::import(new UsersImport, $path);
You will need to let know the library how to map each row from the file into an object for your usage.
-- EDIT
So I would create a model, but you can do the same like this:
class CustomersImport implements ToCollection
{
public function collection(Collection $rows)
{
$data = [];
foreach ($rows as $row)
{
$data[] = array(
'CustomerName' => $row[0],
'Gender' => $row[1],
'Address' => $row[2],
'City' => $row[3],
'PostalCode' => $row[4],
'Country' => $row[5],
);
}
DB::table('tbl_customer')->insert($data);
}
}
So something like this, but debug what does the rows
contains and make sure when you iterate you get the correct key to the correct column in your database.
Related Questions
- → Trait 'IlluminateFoundationBusDispatchesJobs' not found
- → Laravel save CSV file error
- → Importing large CSV files in MySQL using Laravel
- → VBA how do I edit cell contents based on contents of two other cells in same row
- → How to work with PHPWord and LaravelExcel in Laravel 5.1 framework?
- → How to generate heading for columns?
- → VBA Excel run javascript form event
- → Reading excel file in Laravel
- → Remove Links on Excel after rendered from table2excel.js
- → Create JSON with through Excel Hierarchy
- → How to download fetch response in react as file
- → Laravel Excel big export. Server error 500
- → Download XLSX file on anchor link click in Laravel 5.2