LDUBBS
LDUBBS

Reputation: 603

How to skip first row when importing file

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()

Upvotes: 26

Views: 43264

Answers (5)

Rushikesh
Rushikesh

Reputation: 1

Just add WithHeadingRow in the implementation.

use Maatwebsite\Excel\Concerns\WithHeadingRow;

class VotersImport implements ToModel, WithHeadingRow

Upvotes: 0

Manuel Cerón
Manuel Cerón

Reputation: 1

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;
    }

Upvotes: 0

Tushar
Tushar

Reputation: 11

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;
}

Upvotes: 0

Mizhar Raja
Mizhar Raja

Reputation: 194

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();

Upvotes: 1

Robbin Benard
Robbin Benard

Reputation: 1652

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

Upvotes: 73

Related Questions