Laravel 在控制器中使用 Excel 包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34230959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:48:27  来源:igfitidea点击:

Laravel using Excel package inside controller

laravellaravel-5

提问by Hemant

I try to use Excel: @package maatwebsite/excelpackage on laravel inside controller, but i get this error:

我尝试Excel: @package maatwebsite/excel在控制器内部的 laravel上使用包,但出现此错误:

Non-static method Maatwebsite\Excel\Excel::create() 
should not be called statically, assuming $this from incompatible context

my route is:

我的路线是:

Route::get('all_users_report/{format}', 'SystemExportDataController@all_users_report');

my controller is:

我的控制器是:

class SystemExportDataController extends Controller
{
    /**
     * @param $format
     */
    public function all_users_report($format)
    {
        Excel::create('all_users_records', function ($excel) use ($format) {
            $excel->sheet('My Site - all users report', function ($sheet) use ($format) {
                ........
            });
        })->export($format);
    }
}

POST UPDATED 2

帖子更新 2

'providers' => [
    ...
    'Maatwebsite\Excel\ExcelServiceProvider'
],
'aliases' => [
    ...
    'Excel'     => 'Maatwebsite\Excel\Facades\Excel'
],

Error:

错误:

BadMethodCallException in ServiceProvider.php line 234: 
Call to undefined method [package]

UPDATE 3:

更新 3:

Full Class Controller:

全类控制器:

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Maatwebsite\Excel\Excel;

class SystemExportDataController extends Controller
{
    public function all_users_report($format)
    {
        Excel::create('all_users_records', function ($excel) use ($format) {
            $excel->sheet('Epay.Li - all users report', function ($sheet) use ($format) {
                $all_records = ChangeMoney::all();
                $sheet->loadView('layouts.admin.all_users_report',
                    array(
                        'all_records' => $all_records,
                    )
                );
            });
        })->export($format);
    }
}

回答by randomwebdev

You should add 'Excel' => 'Maatwebsite\Excel\Facades\Excel'to your facades array in config/app.phpand 'Maatwebsite\Excel\Excel\ServiceProvider'to your providers in order to use it the way you do.

你应该添加'Excel' => 'Maatwebsite\Excel\Facades\Excel'到您的外墙数组中config/app.php,并'Maatwebsite\Excel\Excel\ServiceProvider'为了使用它,你的方式做你的供应商。

Or you can make an instance of the class using $excel = App::make('excel');and then $excel->create(....

或者您可以使用$excel = App::make('excel');然后创建类的实例$excel->create(...

UPDATE

更新

There are two ways to call the Excel package:

Excel包的调用有两种方式:

One of them is to use the facade, but you have to add 'Excel' => 'Maatwebsite\Excel\Facades\Excel'in your facades array in config/app.phpand then

其中之一是使用门面,但你必须'Excel' => 'Maatwebsite\Excel\Facades\Excel'在你的门面数组中添加config/app.php然后

<?php

namespace App\Http\Controllers;

use App\Http\Requests;

class SystemExportDataController extends Controller
{
    public function all_users_report($format)
    {
        Excel::create('all_users_records', function ($excel) use ($format) {
            $excel->sheet('Epay.Li - all users report', function ($sheet) use ($format) {
                $all_records = ChangeMoney::all();
                $sheet->loadView('layouts.admin.all_users_report',
                    array(
                        'all_records' => $all_records,
                    )
                );
            });
        })->export($format);
    }
}

Note that I removed the second use. Also if you don't want to add the facade in the facades array, you could use it - use Maatwebsite\Excel\Facades\Excel.

请注意,我删除了第二个use. 此外,如果您不想在 facades 数组中添加外观,您可以使用它 - use Maatwebsite\Excel\Facades\Excel

The other way is to make an instance of the package and use that instance, like this:

另一种方法是创建包的实例并使用该实例,如下所示:

   <?php

namespace App\Http\Controllers;
use App\Http\Requests;

class SystemExportDataController extends Controller
{
    public function all_users_report($format)
    {
        $excel = App::make('excel');
        $excel->create('all_users_records', function ($excel) use ($format) {
            $excel->sheet('Epay.Li - all users report', function ($sheet) use ($format) {
                $all_records = ChangeMoney::all();
                $sheet->loadView('layouts.admin.all_users_report',
                    array(
                        'all_records' => $all_records,
                    )
                );
            });
        })->export($format);
    }
}

回答by Danny Thompson

I have found I need to prepend a backslash (signifies the root namespace) to make sure it's using the Facade. This would make your call as follows:

我发现我需要在前面加上一个反斜杠(表示根命名空间)以确保它使用 Facade。这将使您的电话如下:

\Excel::create('all_users_records', function ($excel) use ($format) {
    $excel->sheet('Epay.Li - all users report', function ($sheet) use ($format) {
        $all_records = ChangeMoney::all();
        $sheet->loadView('layouts.admin.all_users_report',
            array(
                'all_records' => $all_records,
            )
        );
    });
})->export($format);

回答by Hemant

Use use Maatwebsite\Excel\Facades\Excelnot use Maatwebsite\Excel\Excel;when you are calling static method like Excel::create()

当您调用静态方法时使用use Maatwebsite\Excel\Facades\Excelnotuse Maatwebsite\Excel\Excel;Excel::create()