php 在 Laravel 中找不到类“App\Http\Controllers\Excel”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30155145/
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-08-25 21:35:08  来源:igfitidea点击:

Class 'App\Http\Controllers\Excel' not found in Laravel

phplaravelnamespaceslaravel-excel

提问by Lulzim

In my controller I have the code as below:

在我的控制器中,我的代码如下:

Excel::create('Laravel Excel', function($excel) {

        $excel->sheet('Excel sheet', function($sheet) {

            $sheet->setOrientation('landscape');

        });

    })->export('xls');

In config/app.php in aliases array i have defined this:

在别名数组的 config/app.php 中,我定义了这个:

'Excel'     => 'Maatwebsite\Excel\ExcelServiceProvider',

I dont know why i cant make it work this library... Any idea?

我不知道为什么我不能让它在这个库中工作......知道吗?

回答by Marcin Nabia?ek

Instead of Excel::createyou should use \Excel::createor add at the beginning of your file after current namespace use Excel;and then you will be able to use Excel::create

而不是Excel::create您应该\Excel::create在当前命名空间之后的文件开头使用或添加,use Excel;然后您就可以使用Excel::create

And the second error is that you used:

第二个错误是您使用了:

'Excel'     => 'Maatwebsite\Excel\ExcelServiceProvider',

and you should use:

你应该使用:

'Excel' => 'Maatwebsite\Excel\Facades\Excel',

instead according to the docs.

而是根据文档

回答by TharinduLucky

Sometimes, clearing configuration cache makes it work

有时,清除配置缓存使其工作

php artisan config:cache

php artisan config:cache

This should work after all you have followed all the instruction correctly but still getting "Class 'App\Http\Controllers\Excel' not found in Laravel"error

在您正确遵循所有说明但仍然收到“Laravel 中未找到 Class 'App\Http\Controllers\Excel'”错误之后,这应该可以工作

回答by Mohammad Sheykholeslam

After all this you need to check whether or not you have this at the top:

毕竟,您需要检查顶部是否有这个:

use Maatwebsite\Excel\Facades\Excel;