Solution:
In Laravel dependency injection is handled by the Container. I’m simplifying, but you can think of the container as a source of objects. If there is a singleton, its stored in the container. Otherwise the container knows how to instantiate objects for you. Whenever Laravel calls a method (like in a controller) or instantiates an object for you it will inspect the constructor and look for type hinted dependencies. If it sees a dependency it knows how to retrieve or create it will do so and pass it in for you.
So when Laravel instantiates the controller it looks at the constructor
public function __construct(UserRepository $user)
{
$this->user = $user;
}
The container uses Type Hinting to see that it requires a UserRepository
so it will instantiate a new one for you. It also does this recursively. So when it creates a new UserRepository
it looks at that constructor and sees that it requires a RoleRepository
so it will instantiate that as well.
TLDR: The service container inspects your dependencies and will instantiate them for you.