25

I'm trying to import an .xlsx file in Laravel version 5.7 using Maatwebsite-excel version 3.1. What I want to achieve is to skip the first row of the file to avoid importing column headers in my database.

I've tried to use version 2 syntax, calling the skip() method.

public function voter_import(Request $request)
{
    if (empty($request->file('file')->getRealPath())) 
    {
        return back()->with('success','No file selected');
    }
    else 
    {
        Excel::import(new VotersImport, $request->file('file'))->skip(1);
        return response('Import Successful, Please Refresh Page');
    }
}

class VotersImport implements ToModel
{
public function model(array $row)
   {
    return new Voter([
      'fname'          =>  $row[0],
      'lname'          =>  $row[1],
      'phone'          =>  $row[2],
      'gender'         =>  $row[3],
      'state'          =>  $row[4],
      'occupation'     =>  $row[5],
      'address'        =>  $row[6],
      'vin'            =>  $row[7],
      'dob'            =>  $row[8],
      'campaign_id'    =>  $row[9],
    ]);
   }
}

error message:

Call to undefined method Maatwebsite\Excel\Excel::skip()

LDUBBS
  • 593
  • 2
  • 8
  • 20

5 Answers5

66

you can implement the StartingRow

use Maatwebsite\Excel\Concerns\WithStartRow;

class VotersImport implements ToModel, WithStartRow
{
    /**
     * @return int
     */
    public function startRow(): int
    {
        return 2;
    }
}

Another option would be to use HeadingRow https://docs.laravel-excel.com/3.1/imports/heading-row.html

Robbin Benard
  • 1,542
  • 11
  • 15
1

Add this code before load the Excel file. it works for me.

config(['excel.import.startRow' => your_number_of_row_to_skip]);

Ex:

$path = $request->file('select_file')->getRealPath();
config(['excel.import.startRow' => 4]);
$data = Excel::load($path)->get();
Mizhar Raja
  • 174
  • 2
  • 12
0

Use setter function like this in your controller

public function voter_import(Request $request)
{
    if (empty($request->file('file')->getRealPath())) 
    {
        return back()->with('success','No file selected');
    }
    else 
    {
        $import = new VotersImport();
        $import->setStartRow(2);
        Excel::import($import, $request->file('file');
        return response('Import Successful, Please Refresh Page');
    }
}

In VotersImport class add setter function and concern WithStartRow

private $setStartRow = 1;
   
public function setStartRow($setStartRow){
     $this->setStartRow = $setStartRow;
}

public function startRow() : int{
     return $setStartRow;
}
Tushar
  • 11
  • 2
0

The Correct Use of code for comment of Tushar

Use $this->setStartRow; instead $setStartRow; in return

Wrong:
    public function startRow() : int{
         return $setStartRow;
    }

Correct:
    public function startRow() : int{
         return $this->setStartRow;
    }
0

Just add WithHeadingRow in the implementation.

use Maatwebsite\Excel\Concerns\WithHeadingRow;

class VotersImport implements ToModel, WithHeadingRow
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77