Laravel Excel stopped working after an upgrade: Excel::load() is gone
Call 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().
Fix it fast
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/excel2Match what you see, then jump to its fix
- 1
Call to undefined method Maatwebsite\Excel\Excel::load()Reading a file with the removed Excel::load()Fix - 2
The same upgrade also breaks export code that calls Excel::create()Building a file with the removed Excel::create()Fix - 3
Old code did Excel::load($path)->get() and used the result directlyOnly the array or collection is needed, not a model importFix
- 1
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
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 5Confirmed by docs.laravel-excel.com
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 excel2Fix
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 tinkerConfirmed by docs.laravel-excel.com
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 says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack 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 fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack 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 fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix