Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Too few arguments to function App\Imports\MyImport

I call my import as follows:

Excel::import(new MyImport(), $filePath);

In MyImport, I try to inject my repository a follows:

 public function __construct(
    protected MyRepository $myRepository,
) {}

/**
* @param Collection $collection
*/
public function collection(Collection $collection)
{
    $this->myRepository->create()...

I get the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Too few arguments to function App\Imports\MyImport::__construct()

What is the proper way to inject repositories in Importer?

>Solution :

When you want to use dependency injection in a class that is not resolved by the service container, you need to manually resolve the dependencies using the app() helper or the resolve() function.

In this code, app(MyImport::class) will create a new instance of MyImport, resolving its dependencies through the service container.

$myImport = app(MyImport::class);
Excel::import($myImport, $filePath);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading