Reputation: 71
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');
}
Upvotes: 2
Views: 11628
Reputation: 687
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!
Upvotes: 1
Reputation: 10061
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;
Upvotes: 0
Reputation: 15085
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;
Upvotes: 1