Cronin
515
8
Laravel

Understanding Method Injection

Posted 3 years ago by Cronin

Right now I have an interface called FriendRepository that only has one implementation: EloquentFriendRepository. A method of that class (which extends Model) is getFriendsOfUser($id).

I want to be able to reference a different model, EloquentUserRepository in order to get the details of each of the friends of the user with $id. The problem is that if I do something like this:

public function getFriendsOfUser($id, UserRepository $user)

...I get errors because the interface only expects one argument.

I thought the point of this was that it wasn't an actual argument, it was an injected dependency that would be handled by the container. In this video Jeffrey even mentions that it doesn't matter what order you put them in. So why is mine being treated as an argument rather than an injected dependency?

Edit: Is my issue just that you can't use method injection when dealing with Models? If so, why is that?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.