skip to Main Content

Please I need quick help. Am trying to import a large excel file in chunk using this code as per the documentation:

Excel::filter('chunk')
    ->load('file.csv')
    ->chunk(250, function($results)
    {
        foreach($results as $row)
        {
            // do stuff
        }
    }); 

However, I am getting this error which I can’t understand.

Call to undefined method MaatwebsiteExcelExcel::filter()

I have checked everywhere online and can’t find the same error anywhere. Where am I going wrong?

2

Answers


  1. Seems like the Excel::filter('chunk') is removed in version 3.1 See the changelog here:

    Excel::filter(‘chunk’) method is removed, chunk filter is automatically added when using chunk reading.

    Also Excel::load() is removed. So you can use Excel::import(..), and it should work.

    Login or Signup to reply.
  2. Please, implement the interface WithChunkReading. And return the chunkSize (in bytes) from the method named chunkSize. For example:

    namespace AppImports;
    
    use AppUser;
    use MaatwebsiteExcelConcernsToModel;
    use MaatwebsiteExcelConcernsWithChunkReading;
    
    class UsersImport implements ToModel, WithChunkReading
    {
        public function model(array $row)
        {
            return new User([
                'name' => $row[0],
            ]);
        }
        
        public function chunkSize(): int
        {
            return 1000;
        }
    }
    

    And call the import method like below from your controller method:

    return Excel::import(new UsersImport, 'users.xlsx');
    

    N.B: This should work from the laravel-excel version 3.1

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search