Ad

Pass Param To Construct

- 1 answer

I write a class like that:

class SERVICE
{
    public function __construct($a, UserRepository $repository) { 
        $this->repository = $repository; $this->relations = [ 
            [
                '\Api\Users\Models\Client', 'clients', '$a'
            ]
        ]; 
        $this->events = [ ]; 
    } 
    ... 
 }

and i used those class like that:

use SERVICE; 

class TEST 
{ 
    public function __construct(SERVICE $service) 
    { 
        $this->service = $service; 
    } 
}

and i have error:

Unresolvable dependency resolving [Parameter #0 [ $a ]]

how can I sen parameter in this way?

Ad

Answer

Laravel automatically can inject only https://laravel.com/docs/5.5/container#automatic-injection. For you case in service has $a parameter which type is not defined. You must be change or your service __construct, or Test class. For example

use SERVICE; 

class TEST 
{ 
    public function __construct(UserRepository $repository) { 
        $this->service = new Service('some-value', $repository);
    }
}

Or change service like this

class SERVICE
{
    public function __construct(UserRepository $repository) { 
        $this->repository = $repository;  
        $this->events = [ ]; 
    } 

    public function setRelations($a) 
    {
         $this->relations = [ 
            [
                '\Api\Users\Models\Client', 'clients', '$a'
            ]
        ];
    }
    ... 
 }

And usage

use SERVICE; 

class TEST 
{ 
    public function __construct(SERVICE $service) 
    { 
        $this->service = $service;
        $this->service->setRelations('some-value'); 
    } 
}
Ad
source: stackoverflow.com
Ad