0

I have created a service in app/services/KDataService.php that looks like this:

class KDataService
{
    /** @var  string */
    private $license;

    /** @var  string */
    private $owner;

    /** @var string  */
    private $accessToken;

    public function __construct($owner, $license)
    {
        $this->owner = $owner;
        $this->license = $license;

        ...

    }

       ...

}

In one of my controller I try to inject this service with the dependency injection pattern but I get the following error:

Unresolvable dependency resolving [Parameter #0 [ $owner ]] in class App\Services\KDataService

My controller:

use App\Services\KDataService;

class DamagePointController extends Controller
{
    /** @var  KDataService $kDataService */
    private $kDataService;

    /**
     * Instantiate a new controller instance.
     *
     * @param KDataService $kDataService
     */
    public function __construct(KDataService $kDataService)
    {
        $this->kDataService = $kDataService;
    }  

    ...

}

Anyone knows how I can pass my $owner and $license?

3
  • Laravel version? Commented Oct 13, 2017 at 12:59
  • @FreeLightman 5.5, I'll edit OP too Commented Oct 13, 2017 at 13:06
  • What are the $owner and $licence types? Laravel don't know what to inject to build your KDataService. Commented Oct 13, 2017 at 13:12

1 Answer 1

4

The problem is that your service has arguments but you don't specify them. There are several ways to do this.

Using service provider:

namespace App\Providers;

use Riak\Connection;
use Illuminate\Support\ServiceProvider;

class kDataServiceServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(KDataService::class, function ($app) {
            return new KDataService(getOwner(), getLicense());
        });
    }
}

bind could be change to other methods. See Service Container docs.

Using app to make instanse:

/* Controller __construct */
$this->kDataService = \App::make(KDataService::class, [getOwner(), getLicense()]);

Simply create class instance

/* Controller __construct */
$this->kDataService = new KDataService(getOwner(), getLicense());

Note: getOwner and getLicense change to your logic. Usually you can retrieve it within controller or from $app.

Generally what you need to resolve the issue is to read about service container and service providers in docs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.