Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel Excel stopped working after an upgrade: Excel::load() is gone

ErrorCall to undefined method Maatwebsite\Excel\Excel::load()

Short answer

Maatwebsite Laravel Excel 3 removed Excel::load(). The call fails because your code was written for version 2.x. Replace it with an import class and Excel::import(), and replace Excel::create() with an export class and Excel::download() or Excel::store().

  • Applies to Laravel 12, PHP 8.3, maatwebsite/excel 3.1
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which major version of the package is installed. A 3.x version means every 2.x style call must be rewritten.

    composer show maatwebsite/excel
  2. 2Match what you see, then jump to its fix

    1. 1Call to undefined method Maatwebsite\Excel\Excel::load()Reading a file with the removed Excel::load()Fix
    2. 2The same upgrade also breaks export code that calls Excel::create()Building a file with the removed Excel::create()Fix
    3. 3Old code did Excel::load($path)->get() and used the result directlyOnly the array or collection is needed, not a model importFix

What does the error look like?

  • Call to undefined method Maatwebsite\Excel\Excel::load()

Cause 1 of 3

Reading a file with the removed Excel::load()

1Check

Search the project for the old call. The 3.0 upgrade guide says Excel::load() is replaced by Excel::import() with an import object.

grep -rn "Excel::load" app/ routes/

2Fix

Write an import class, then pass an instance of it and the file to Excel::import(). For an uploaded file, pass the file from the request.

// app/Imports/UsersImport.php
namespace App\Imports;

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

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

// controller
use Maatwebsite\Excel\Facades\Excel;
Excel::import(new UsersImport, $request->file('your_file'));

3Verify

Upload a small sheet and check that the rows reach the table.

php artisan tinker --execute="echo App\Models\User::count();"

Confirmed by docs.laravel-excel.com

Did this fix it?

Cause 2 of 3

Building a file with the removed Excel::create()

1Check

The upgrade guide says Excel::create() is replaced by Excel::download() or Excel::store() with an export object.

grep -rn "Excel::create" app/ routes/

2Fix

Move the sheet-building code into an export class and hand an instance to Excel::download(). Follow the upgrade guide's mapping for your old options.

use Maatwebsite\Excel\Facades\Excel;
return Excel::download(new UsersExport, 'users.xlsx');

3Verify

Open the download in a spreadsheet program.

composer show maatwebsite/excel | head -n 5

Confirmed by docs.laravel-excel.com

Did this fix it?

Cause 3 of 3

Only the array or collection is needed, not a model import

1Check

Version 3 can still hand back the rows without saving them. The import basics page documents toArray and toCollection, which accept an import class.

grep -rn "->get()" app/Http/Controllers | grep -i excel

2Fix

Call toArray or toCollection with the import class. Keep in mind the docs say this bypasses ToModel, so large files cost more memory.

$rows = Excel::toArray(new UsersImport, 'users.xlsx');
$collection = Excel::toCollection(new UsersImport, 'users.xlsx');

3Verify

dd() the result and compare it to your sheet.

php artisan tinker

Confirmed by docs.laravel-excel.com

Did this fix it?

How do I stop it coming back?

  • Read the package's upgrade guide before raising a major version in composer.json.
  • Pin the major version (^3.1) so composer update does not jump to a version that changes the API.
  • Cover import and export with a feature test that uses a small fixture file.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.