Laravel-excel: Is there a way to skip the first row (header)?

Created on 2 May 2019  ·  5Comments  ·  Source: Maatwebsite/Laravel-Excel

Versions

  • PHP version: 7.2.17
  • Laravel version: 5.8.15
  • Package version: 3.1

Description

Is there a way to skip the header row and start importing from the 2nd row?

Like:

class UsersImport implements ToModel
{
    /**
     * @param array $row
     *
     * @return User|null
     */
    public function model(array $row)
    {
        return new User([
           'name'     => $row[0],
           'email'    => $row[1], 
           'password' => Hash::make($row[2]),
        ]);
    }
}

But starting from the second row? So I don't have to worry about my header names, just the order of my columns.

Most helpful comment

I solved it with

class UsersImport implements ToModel, WithStartRow
{
    ...

    /**
     * @return int
     */
    public function startRow(): int
    {
        return 2;
    }
}

All 5 comments

I solved it with

class UsersImport implements ToModel, WithStartRow
{
    ...

    /**
     * @return int
     */
    public function startRow(): int
    {
        return 2;
    }
}

I know you've solved this already but there's a built-in way to do this: WithHeadingRow

I know you've solved this already but there's a built-in way to do this: WithHeadingRow

But if you use WithHeadingRow you cannot access row items by index

nice!

I know is closed, but if you use @lcardosozago 's method, don't forget to add:

use Maatwebsite\Excel\Concerns\WithStartRow;

Was this page helpful?
0 / 5 - 0 ratings