I'm trying to understand Repository Pattern in Laravel but it doesn't work. It gives me an error "Target [IUserRepository] is not instantiable.". The provider works fine after I added this 'Repositories\User\UserServiceProvider' but the problem is in the UserServiceProvider.php . If I remove the namespace, it works fine. Please help, thanks.
Asked
Active
Viewed 132 times
1 Answers
1
Well you have to type hint it with it's full namespace glory:
public function __construct(Repositories\User\IUserRepository $user){
A use
statement will probably do as well:
use Repositories\User\IUserRepository;
public function __construct(IUserRepository $user){

lukasgeiter
- 147,337
- 26
- 332
- 270
-
I've got this error "Class 'Repositories\User\User' not found". After adding Repositories\User\IUserRepository. Thanks for helping. – Rbex Feb 04 '15 at 07:40
-
I think it's in the model issue. I'll have to finish the tutorials and get back on this later. Thanks. – Rbex Feb 04 '15 at 07:47
-
I can't really solve this one. I hope you can help me. It's not a model issue. – Rbex Feb 04 '15 at 07:58
-
In what namespace is your `User` model? – lukasgeiter Feb 04 '15 at 08:09
-
It's just the default user.php . No namespace, I mean. it's just use use Illuminate\Auth\UserTrait; use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableTrait; use Illuminate\Auth\Reminders\RemindableInterface; – Rbex Feb 04 '15 at 08:10
-
Then reference it in your repository like `\User::all()`. Or add `use User;` at the top. – lukasgeiter Feb 04 '15 at 08:12
-
I've got it. I didn't add it in the model, I add it in the UserRepository and it works. I don't know why but if it's not too much to ask. I hope you can give me an idea. Thanks. – Rbex Feb 04 '15 at 08:25
-
1The problem when is when doing `User::all()` you reference the `User` in the current namespace (`Repositories\User`). That's why it works when you're working in the global namespace (let's say in the routes file) but not in your repository. So you either have to tell it that it is an "absolute" path to your class by prepending a backslash `\User` or add the `use` statement (they are never relative to the current namespace) – lukasgeiter Feb 04 '15 at 08:30