Call to undefined method Maatwebsite\Excel\Excel::load()
Asked Answered
M

7

22

I'm trying to import excel file (.xlsx) using maatwebsite 3.0. How to fix This error

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

My controller

public function importsave(Request $request)
{
   if($request->hasFile('excel'))
    {
        $path = $request->file('excel')->getRealPath();
        $data= Excel::load($path, function($reader) {})->get();
        if(!empty($data) && $data->count())
        {
            foreach($data->toArray() as $key=>$value)
            {
                if(!empty($value))
                {
                    Employee::insert($value);
                }
            }
        }
    }
}
Meliamelic answered 25/3, 2018 at 6:20 Comment(0)
G
34

Version 3.0 of that package doesn't handle imports yet. Release date for this feature is unknown. See this post for more details: https://medium.com/@maatwebsite/laravel-excel-lessons-learned-7fee2812551

I suggest you switch to version 2.*.

Else you want to continue further ALL Laravel Excel 2.* methods are deprecated and will not be able to use in 3.0 .

   Excel::load() is removed and replaced by Excel::import($yourImport)
    Excel::create() is removed and replaced by Excel::download/Excel::store($yourExport)
    Excel::create()->string('xlsx') is removed an replaced by Excel::raw($yourExport, Excel::XLSX)

3.0 provides no convenience methods for styling, you are encouraged to use PhpSpreadsheets native methods.

Georgia answered 25/3, 2018 at 6:34 Comment(4)
@RioAditya could you mark the question as answered?Georgia
Version 3.1 is now available with import functionality docs.laravel-excel.com/3.1/importsLegalize
My whole project is in laravel 6.18 and now I can't install maatwebsite/excel 2.1 or any closer version, please helpAre
@Are did you installed maatwebsite/excel 2.1 and checked?Southsoutheast
C
12

Hi there in version 3 the load method was remove so switch back to version two like so try using this command,

composer require "maatwebsite/excel:~2.1.0"

Christoper answered 2/8, 2018 at 6:26 Comment(3)
its actually "maatwebsite/excel":"~2.1.0"Intern
Gives an error "- Conclusion: remove laravel/framework v5.8.18" So it might not be compatible with new versions.Pastel
I have v2.1 maatwebsite/excel and 5.8 laravel do I need to downgrade my laravel version? \Kibitka
R
12

ALL Laravel Excel 2.* methods are deprecated and will not be able to use in 3.0 .

Excel::load() is removed and replaced by Excel::import($yourImport)
Excel::create() is removed and replaced by Excel::download/Excel::store($yourExport)
Excel::create()->string('xlsx') is removed an replaced by Excel::raw($yourExport, Excel::XLSX)

3.0 provides no convenience methods for styling, you are encouraged to use PhpSpreadsheets native methods.

Ruthi answered 27/7, 2019 at 10:19 Comment(1)
this cpuld be the marked answerBriarroot
S
11

Dont panic :) Just do like this

Excel::toArray([],$filePath);

Just pass an empty array as the first parameter

Suffumigate answered 1/12, 2021 at 10:30 Comment(2)
works like a charm on package version 3.1 (PHP 8.1, Laravel 9)Repudiate
This must be a smartest and perfect answer among all.Crutchfield
S
9

^3.0 versions of maatwebsite/excel does not support loads.
Delete the config/excel.php file first .

Downgrade the maatwebsite/excel version by changing your composer.json file from "maatwebsite/excel": "^3.1", to "maatwebsite/excel": "~2.1.0", and performing 'composer update'.

And if you get an error:

Undefined class constant 'XLSX' in 2.1 version

this is what you have to do.
Delete the config/excel.php before downgrading then perform the composer update.

Semicentennial answered 25/1, 2020 at 5:21 Comment(1)
Doesnt work for laravel 6, its dependency doesnt matchRedwine
A
3

Version 3.0 of Laravel Excel doesn't handle imports.

You could also use an alternative package that works with import such as:

Both handles import.

You could also switch to version 2, but it means use an old version of a lib.

Aphaeresis answered 24/9, 2018 at 15:46 Comment(0)
E
0

Don't Panic Just Do It ... :)

$data = Excel::toArray([], $request->file('file_file'))[0];
            $headers = $data[0];
            $final = [];

            foreach ($data as $datum) {
                $final[] = array_combine($headers, $datum);
            }

            $counter = 0;
            foreach ($final as $row) {

                $service = Service::where('code', $row['code'])->orderBy('status', 'desc')->first();
                if (!is_null($service)) {
                    dump($service->code);
                    $service->cost = $row['cost'];
                    $service->price = $row['price'];
                    $service->discount = $row['discount'];
                    $service->vat = $row['vat'];
                    $service->sbs_code = $row['sbs_code'];
                    $service->sbs_description = $row['sbs_description'];
                    $service->is_package = $row['is_package'];
                    $service->save();
                    $counter++;
                }

            }
Exclamatory answered 12/6 at 2:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.