0
votes

I make export classes exportable in the App/Exports like this :

<?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();
    }
}

I call from controller like this :

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

There exist error like this :

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

How can I solve this error?

I get export excel from this tutorial :https://laravel-excel.maatwebsite.nl/docs/3.0/export/basics

1
Pass an argument to new ItemsExport ?Jerodev
@Jerode Yes. Of courseSuccess Man

1 Answers

1
votes

In order to use Laravel's dependency injection you need to construct the new instance via the application container:

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

This assumes that the applications knows how to construct the ItemRepository

Alternatively if this is in a controller action you can just inject it e.g.

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