LoveCoding
LoveCoding

Reputation: 1211

How can I get repository instance?

I am quite new in Laravel repository.

I am using l5-repository package.

When I created resource controller, it created as following:

    public function __construct(MyRepository $repository, MyValidator $validator)
    {
        $this->repository = $repository;
        $this->validator  = $validator;
    }

I could get MyRepository instance with $this->repository in this controller.

But I need to use another repository in this controller.

I tried to get like this but failed.

$anotherRepo = new AnotherRepo(); // I think AnotherRepo` is interface.

I was going to get $anotherRepo->all() data but couldn't get $anotherRepo instance.

Please help me how can I get repository instance.

Upvotes: 0

Views: 84

Answers (1)

Andy Song
Andy Song

Reputation: 4684

You just pass it into constructor and then access it like others.

public function __construct(AnotherRepo $another, MyRepository $repository, MyValidator $validator)
{
    $this->another = $another;
    $this->repository = $repository;
    $this->validator  = $validator;
}

And then get it $this->another.

Upvotes: 1

Related Questions