Laravel-excel: How can i count the number of rows the imported file has if it's ToModel

Created on 8 Nov 2018  路  1Comment  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

Versions

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

    Description

i wanna count the number of rows an imported file have

Additional Information

use a toModel type of import

Most helpful comment

Just keep a counter property in your import class.

namespace App\Imports;

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

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

>All comments

Just keep a counter property in your import class.

namespace App\Imports;

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

class UsersImport implements ToModel
{
    public $count = 0;
    public function model(array $row)
    {
        ++$this->count;
        return new User([
            'name' => $row[0],
        ]);
    }
}
Was this page helpful?
0 / 5 - 0 ratings