我如何解决导出excel laravel中的"too few arguments to function App\Exports\ItemsExport::__construct(), 0 passed"?

How can I solve "too few arguments to function App\Exports\ItemsExport::__construct(), 0 passed" in the export excel laravel?

我在 App/Exports 中使导出 类 可导出,如下所示:

<?php
namespace App\Exports;
use App\Repositories\ItemRepository;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
class ItemsExport implements FromCollection
{
    use Exportable;
    protected $itemRepository;
    public function __construct(ItemRepository $itemRepository)
    {
        $this->itemRepository = $itemRepository;
    }
    public function collection()
    {
        return $this->itemRepository->getItem();
    }
}

我这样从控制器调用:

return (new ItemsExport)->download('items.xlsx');

存在这样的错误:

too few arguments to function App\Exports\ItemsExport::__construct(), 0 passed

如何解决这个错误?

我从本教程导出 excel :https://laravel-excel.maatwebsite.nl/docs/3.0/export/basics

为了使用 Laravel 的依赖注入,您需要通过应用程序容器构建新实例:

return app()->make(ItemsExport::class)->download('items.xlsx');

这假定应用程序知道如何构造 ItemRepository

或者,如果这是在控制器操作中,您可以将其注入,例如

public function controllerAction(ItemsExport $exporter) {
     return $exporter->download('items.xlsx');
}