2

My program produces this error but I don't know why:

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

Code:

public function exportExcel(){
    $users = User::all();
    $user_array = array('Name', "Email");
    foreach($users as $user){
        $user_array[] = array(
            'Name' => $user->name,
            'Email' => $user->email,
        );

    }
    Excel::create('User Data', function ($excel) use ($user_array){
        $excel->setTitle('User Data');
        $excel->sheet('User Data', function($sheet) use ($user_array){
         $sheet->fromArray($user_array);
        });
       })->download('xls');
    return redirect()->route('admin.page');
}
2
  • Which version of Laravel Excel you have used? Commented Dec 15, 2018 at 14:52
  • It is version 3.1.4 Commented Dec 15, 2018 at 15:14

3 Answers 3

1

According to this docs (Officinal document Here):

You may do this by using the make:export command.

php artisan make:export UsersExport --model=User

If you prefer to create the export manually, you can create the following in App/Exports:

<?php

namespace App\Exports;

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

class UsersExport implements FromCollection
{
    public function collection()
    {
        return User::all();
    }
}

In your controller you can call this export now:

use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;

class UsersController extends Controller 
{
    public function export() 
    {
        return Excel::download(new UsersExport, 'users.xlsx');
    }
}

Find your users.xlsx in your downloads folder!

Sign up to request clarification or add additional context in comments.

3 Comments

which version of Excel library r u using? u can find it in your composer.json file
3.1.4 version is what I am using
I know this question might not stand, but can't we make excel from custom arrays. I mean to say, if they are not models. Created based on some calculations?
1

try this one

you are using Maatwebsite\Excel\Excel class instead of use import facade Excel

use Maatwebsite\Excel\Facades\Excel;

other wise used

use Excel;

Comments

0

Add the ServiceProvider in config/app.php

'providers' => [
    Maatwebsite\Excel\ExcelServiceProvider::class,
]

Add the Facade in config/app.php

'aliases' => [
    ...
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]

Laravel Excel can be used in a various of ways.

Usage:

use Maatwebsite\Excel\Excel;

2 Comments

ALL Laravel Excel 2.* methods are deprecated and will not be able to use in 3.0 . Excel::create() is removed and replaced by Excel::download/Excel::store($yourExport)
When I try to use store or download it says that I am using them statically, what does this mean?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.