0

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

moses toh
  • 12,344
  • 71
  • 243
  • 443

1 Answers1

1

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');
} 
apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • Your code works. But your code is not in the documentation. Can you show your script used to open excel? No download. I tried to use the code in the documentation, same error – moses toh Jul 27 '18 at 09:36
  • So I mean, Can you update your answer, how to open excel? – moses toh Jul 27 '18 at 09:48
  • I try like this : `return $exporter->store('items.xlsx');` there exist error : `The Response content must be a string or object implementing __toString(), "boolean" given.` – moses toh Jul 27 '18 at 09:55
  • `store` ? why would use store to send a response? You originally used `download` which looks more correct. Also this code has nothing to do with laravel-excel but with fundamentals of laravel itself. – apokryfos Jul 27 '18 at 10:10
  • I want when the function is executed, it will open the excel file. No download – moses toh Jul 27 '18 at 11:45
  • I don't think an HTTP response can coerce the browser into launching an arbitrary application without the user actually setting it up to do that – apokryfos Jul 27 '18 at 11:47
  • That's why I asked how to set it up? If I can setting that, I will not ask – moses toh Jul 27 '18 at 11:54
  • Seems you can help me. Look at this : https://stackoverflow.com/questions/51572197/how-can-i-pass-parameter-in-the-laravel-excel – moses toh Jul 28 '18 at 14:19