I'm trying to add export functionalities to my Laravel app. I'd like to export db data into an excel spreadsheet. I'm using the Maatwebsite package. I'm using Laravel 7.12 and 3.1.19 of the Maatwebsite package.
I'm getting the following error when trying to export the data:
Call to undefined method Maatwebsite\Excel\Excel::create()
I added the foloowing use statement to my controller:
use Maatwebsite\Excel\Facades\Excel;
And registered the followings in config/app.php
Maatwebsite\Excel\ExcelServiceProvider::class,
in providers and
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
in the aliases section
Here is my function from the controller file:
public function excel() {
$subscribers = DB::table('subscribers')->get()->toArray();
// Use this for excel spreadsheet header
$subscriber_array[] = array('Name', 'Email');
// Convert subscriber data from php object to array and store them under $subscriber_array
foreach($subscribers as $subscriber) {
$subscriber_array[] = array(
'Name' => $subscriber->name,
'Email' => $subscriber->email
);
}
// "Subscriber Data" will be the name of the generated excel file
Excel::create('Subscriber Data', function($excel) use ($subscriber_array) {
$excel->setTitle('Subscriber Data');
$excel->sheet('Subscriber Data', function($sheet) use ($subscriber_array) {
$sheet->fromArray($subscriber_array, null, 'A1', false, false);
});
})->download('xlsx');
}