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

Call to undefined method Maatwebsite\Excel\Facades\Excel::download()

我的程序产生了这个错误,但我不知道为什么。我仍然收到此错误,即使我在 config/app.php

中添加了服务提供商和别名

Call to undefined method Maatwebsite\Excel\Facades\Excel::download()

UserController.php

use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;

class UserController extends Controller
{
    private $excel;

    public function __construct(Excel $excel)
    {
        $this->excel = $excel;
    }
    
    public function export()
    {
        return $this->excel->download(new UsersExport, 'users.xlsx');
    }

}

UsersExport.php

<?php

namespace App\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use Modules\User\Entities\User;

class UsersExport implements FromView, ShouldAutoSize, WithEvents
{
    use Exportable;

    private $fileName  = "user.xlsx";

    /**
     * @return View
     */
    public function view(): View
    {
        return view('users::admin.export', [
            'users' => User::all()
        ]);
    }

    /**
     * @return array
     */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true);
            },
        ];
    }
}

再次确认您已在config/app中添加服务提供商和别名。php

然后尝试 运行:

php artisan config:clear
php artisan config:cache