Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to skip the first row (header)? #2185

Closed
lcardosozago opened this issue May 2, 2019 · 5 comments
Closed

Is there a way to skip the first row (header)? #2185

lcardosozago opened this issue May 2, 2019 · 5 comments

Comments

@lcardosozago
Copy link

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.

@lcardosozago lcardosozago changed the title Is there a way to 'jump' the header row? Is there a way to skip the header row? May 2, 2019
@lcardosozago lcardosozago changed the title Is there a way to skip the header row? Is there a way to skip the first row (header)? May 2, 2019
@lcardosozago
Copy link
Author

lcardosozago commented May 2, 2019

I solved it with

class UsersImport implements ToModel, WithStartRow
{
    ...

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

@nexxai
Copy link

nexxai commented May 3, 2019

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

@eddiebui
Copy link

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

@xumingpeng
Copy link

nice!

@catalinmoraru82
Copy link

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

use Maatwebsite\Excel\Concerns\WithStartRow;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants