Ad

Too Few Arguments To Function Maatwebsite\Excel\Excel::import(), 1 Passed And At Least 2 Expected

- 1 answer

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?

Ad

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.

Ad
source: stackoverflow.com
Ad