17

I'm getting this error:

BindingResolutionException in compiled.php line 1029:

Target [App\Models\Contracts\Repositories\IUserRepository] is not instantiable.

My code is as follows:

Interface:

namespace App\Models\Contracts\Repositories;

use App\Models\Objects\DTO\User;

interface IUserRepository
{
    function Create( User $user );
}

Concrete:

namespace App\Models\Concrete\Eloquent;

use App\Models\Contracts\Repositories\IUserRepository;
use App\Models\Objects\DTO\User;

class EqUserRepository implements IUserRepository
{
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    public function Create( User $user )
    {
        return User::create( [
                    'first_name' => $user->first_name,
                    'last_name' => $user->last_name,
                    'username' => $user->username,
                    'email' => $user->email,
                    'password' => bcrypt( $user->password ),
                ] );
    }

}

Service Provider:

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * This service provider is a great spot to register your various container
     * bindings with the application. As you can see, we are registering our
     * "Registrar" implementation here. You can add your own bindings too!
     *
     * @return void
     */
    public function register()
    {

            $this->app->bind(
                    'App\Models\Contracts\Repositories\IUserRepository', 
                    'App\Models\Concrete\Eloquent\EqUserRepository'
            );
    }

}

Controller:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

use App\Models\Contracts\Repositories\IUserRepository;
use App\Models\Objects\DTO\User;

class AuthController extends Controller
{
    protected $auth;
    private $userRepository;

    public function __Construct(  
            Guard $auth, 
            IUserRepository $userRepo )
    {
    ...

Folder structure

enter image description here

I have also seen that I may need to declare the namespaces in my composer.json, So i have tried the following as well as just the above:

"autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "App\\Models\\Concrete\\Eloquent\\": "app/Models/Concrete/Eloquent/",
            "App\\Models\\Contracts\\Repositories\\": "app/Models/Contracts/Repositories/",
            "App\\Models\\Objects\\DTO\\": "app/Models/Objects/DTO/"
        }
    },

and then ran composer dump-autoload

Any ideas what I am forgetting to do?

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • I've moved the concrete repository to the services folder and given it that namespace. Still the same error. – Jimmyt1988 Feb 19 '15 at 01:21

4 Answers4

21

I noticed the compiled.php was not being updated.

Run this function in cmd line on the root folder of your project:

php artisan clear-compiled
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
7

If you also run in below error :

BindingResolutionException in Container.php line 749: Target [App\Contracts\TestContract] is not instantiable.

Clear your config cache with :

php artisan config:clear
Jimmy Obonyo Abor
  • 7,335
  • 10
  • 43
  • 71
  • Seems you can help me. Look at this : https://laracasts.com/discuss/channels/laravel/how-can-i-solve-target-apprepositoriesnewsreposi – moses toh Aug 30 '17 at 09:57
0

In my case the error was, I added protected keyword in controller __construct method

Before

protected function __construct()

After

public function __construct()

Because I am using polices resource and I added $this->authorizeResource(Task::class); in my construct method, to apply it on all methods.

Hadayat Niazi
  • 1,991
  • 3
  • 16
  • 28
-1

I had same error solved by adding repository in app/providers/AppServiceProvider in register method like the below.

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

public function register()
    {
        $this->app->register(RepositoryServiceProvider::class); // here
    }
}
Siraj Ali
  • 526
  • 6
  • 13