调用Laravel 5.8中未定义的方法Maatwebsite \ Excel \ Excel :: create()

时间:2019-05-29 14:44:08

标签: laravel maatwebsite-excel

我正在使用Laravel 5.8和maatwebsite / excel 3.1导出到Excel,但出现错误。

  

调用未定义的方法Maatwebsite \ Excel \ Excel :: create()

我已经在Controller和View中编写了导出代码

config / 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,但出现此错误。

  

调用未定义的方法Maatwebsite \ Excel \ Excel :: create()

1 个答案:

答案 0 :(得分:0)

create方法已被删除。您必须使用以下之一:

Excel::download($yourExport);
Excel::store($yourExport);

如升级指南中所述:

  

Excel :: create()已删除,并由Excel :: download / Excel :: store($ yourExport)代替

来源:https://docs.laravel-excel.com/3.1/getting-started/upgrade.html#upgrading-to-3-from-2-1