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:
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);