Я использую Laravel 5.8 и maatwebsite/excel 3.1 для экспорта в Excel, но получил ошибку.
Call to undefined method Maatwebsite\Excel\Excel::create()
Я написал код экспорта в Controller и View
конфиг/app.php
/*
* Package Service Providers...
*/
Maatwebsite\Excel\ExcelServiceProvider::class,
//Class Aliases
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
Контроллер
use Excel;
public function msisdnExport()
{
$msisdns = User::select(
"phone"
)
->get();
// Initialize the array which will be passed into the Excel
// generator.
$msisdnsArray = [];
// Define the Excel spreadsheet headers
$msisdnsArray[] = ['MSISDN'];
// Convert each member of the returned collection into an array,
// and append it to the payments array.
foreach ($msisdns as $msisdn) {
$msisdnsArray[] = $msisdn->toArray();
}
// Generate and return the spreadsheet
// Excel::create('MSISDN', function($excel) use ($msisdnsArray) {
Excel::download('MSISDN', function($excel) use ($msisdnsArray) {
// Set the spreadsheet title, creator, and description
$excel->setTitle('MSISDN');
$excel->setCreator('Developers')->setCompany('Cloud Africa');
$excel->setDescription('users msisdn file');
// Build the spreadsheet, passing in the payments array
$excel->sheet('sheet1', function($sheet) use ($msisdnsArray) {
$sheet->fromArray($msisdnsArray, null, 'A1', false, false);
});
})->download('xlsx');
}
Вид
<a href = "{{ route('msisdn-export') }}" class = "btn btn-block btn-primary" style = "margin-right: 15px;"><i class = "fa fa-file-excel-o"></i> Excel</a>
Когда я нажимаю на Excel в поле зрения, он предполагает экспорт в Excel, но получил эту ошибку.
Call to undefined method Maatwebsite\Excel\Excel::create()
Метод create
был удален. Вы должны использовать одно из следующего:
Excel::download($yourExport);
Excel::store($yourExport);
Как указано в руководстве по обновлению:
Excel::create() is removed and replaced by Excel::download/Excel::store($yourExport)
Источник: https://docs.laravel-excel.com/3.1/getting-started/upgrade.html#upgrading-to-3-from-2-1
@ user11352561 Вы читали руководство по обновлению? Вы не можете просто переименовать функцию. Взгляните на это здесь: github.com/Maatwebsite/Laravel-Excel/issues/1799
Я изменил его, но получил другую ошибку. > Аргумент 2, передаваемый в Maatwebsite\Excel\Excel::download(), должен иметь тип string, заданный объект, вызываемый в C:\xampp\htdocs\reporting-soccerrave\vendor\laravel. Посмотреть обновление