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

How to get current row number #2001

Closed
dalholm opened this issue Jan 18, 2019 · 10 comments
Closed

How to get current row number #2001

dalholm opened this issue Jan 18, 2019 · 10 comments

Comments

@dalholm
Copy link

dalholm commented Jan 18, 2019

Prerequisites

Versions

  • PHP version: 7.2
  • Laravel version: 5.7
  • Package version: 3.1

Description

Im using ToModel in my import and need to store the current row number.
How do i do that? :)

namespace App\Imports;

use App\User;
use Maatwebsite\Excel\Concerns\ToModel;

class UsersImport implements ToModel
{
    public function model(array $row)
    {
        return new User([
            'name' => $row[0],
            'row_number' => '?????'
        ]);
    }
}
@patrickbrouwers
Copy link
Member

@dalholm you can keep state in your import object.

class UsersImport implements ToModel
{
    private $row = 0;

    public function model(array $row)
    {
        return new User([
            'name' => $row[0],
            'row_number' => ++$this->row,
        ]);
    }
}

@dalholm
Copy link
Author

dalholm commented Jan 18, 2019

@patrickbrouwers so simple, worked like a charm. Thanks! 👍

@dalholm dalholm closed this as completed Jan 18, 2019
@weiwait
Copy link

weiwait commented Jan 9, 2020

@dalholm you can keep state in your import object.

class UsersImport implements ToModel
{
    private $row = 0;

    public function model(array $row)
    {
        return new User([
            'name' => $row[0],
            'row_number' => ++$this->row,
        ]);
    }
}

So, how do it at chunk reading?

@benedict-w
Copy link

@weiwait yes - any way of checking the row count in a chunked import?

@weiwait
Copy link

weiwait commented Apr 9, 2020

@weiwait yes - any way of checking the row count in a chunked import?
I'm using Cache Dirve to record
1586418540(1)
current chunk

@PLP-GTR
Copy link

PLP-GTR commented Nov 12, 2021

Since this is still showing up in search results and nobody mentioned it yet:

There is official documentation on how to keep track of the row number using the RemembersRowNumber trait:
https://docs.laravel-excel.com/3.1/imports/chunk-reading.html#keep-track-of-the-row-number

...
use Maatwebsite\Excel\Concerns\RemembersRowNumber;
...

class PizzaImport implements ToModel, WithChunkReading
{
    use RemembersRowNumber;

    public function model(array $row)
    {
        return new Pizza([
            'row_number' => $this->getRowNumber(),
            'name' => $row[0],
            ...
        ]);
    }
    
    ...
}

Only usage is with models, though, no collections.

@muarachmann
Copy link

Is there a way to get the same for an export? I can't find it in the docs https://docs.laravel-excel.com/3.1/exports/exportables.html

@itxshakil
Copy link

I did this and working perfectly.

class UsersImport implements ToModel
{
    private $rowNumber = 1;

    public function model(array $row)
    {
        return new User([
            'name' => $row[0],
            'row_number' => $this->rowNumber++,
        ]);
    }
}

@Beybala1
Copy link

@dalholm you can keep state in your import object.

class UsersImport implements ToModel
{
    private $row = 0;

    public function model(array $row)
    {
        return new User([
            'name' => $row[0],
            'row_number' => ++$this->row,
        ]);
    }
}

@dalholm you can keep state in your import object.

class UsersImport implements ToModel
{
    private $row = 0;

    public function model(array $row)
    {
        return new User([
            'name' => $row[0],
            'row_number' => ++$this->row,
        ]);
    }
}

Worked for me thank you

@snapey
Copy link

snapey commented Mar 17, 2024

Does not work when skipEmptyRows is used. I want to feedback to the user which row they have an issue with but it gets out of sync if they have any empty rows.

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

9 participants