【问题标题】:Call to undefined method Maatwebsite\Excel\Excel::create() in Laravel 5.8在 Laravel 5.8 中调用未定义的方法 Maatwebsite\Excel\Excel::create()
【发布时间】:2019-10-15 05:20:50
【问题描述】:

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

调用未定义的方法 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 但出现此错误。

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

【问题讨论】:

    标签: laravel maatwebsite-excel


    【解决方案1】:

    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

    【讨论】:

    • 我更改了它,但又出现了一个错误。 > 传递给 Maatwebsite\Excel\Excel::download() 的参数 2 必须是字符串类型,给定对象,在 C:\xampp\htdocs\reporting-soccerrave\vendor\laravel 中调用。查看更新
    • @user11352561 您是否阅读了升级指南?您不能简单地重命名函数。在这里看看这个:github.com/Maatwebsite/Laravel-Excel/issues/1799
    猜你喜欢
    • 2019-11-02
    • 2018-10-28
    • 2018-11-09
    • 2021-02-06
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多