Laravel-excel: How to get current row number

Created on 18 Jan 2019  路  5Comments  路  Source: Maatwebsite/Laravel-Excel

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' => '?????'
        ]);
    }
}

Most helpful comment

@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?

All 5 comments

@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,
        ]);
    }
}

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

@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?

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daraghoshea picture daraghoshea  路  3Comments

octoxan picture octoxan  路  3Comments

dr3ads picture dr3ads  路  3Comments

amine8ghandi8amine picture amine8ghandi8amine  路  3Comments

matthewslouismarie picture matthewslouismarie  路  3Comments