Target [Repository] is not instantiable while building [Controller, Service]

1

I am developing a system that when sending data by the form is returning me this error :

  

BindingResolutionException in Container.php line 763: Target   [Church \ Repositories \ MemberRepository] is not instantiable while   building [Church \ Http \ Controllers \ MemberController,   Church \ Services \ MemberService].

I have already done a search and the solutions that are presented to me are related to namespace , but I have already looked at it several times, deleted it, created it again, etc. and the error persists. I've been stuck here for two days. Using Laravel 5.3 and prettus repository 2.6 .

Follow other documents:

Model Membro.php

<?php

namespace Igreja\Entities;

use Illuminate\Database\Eloquent\Model; 
use Prettus\Repository\Contracts\Transformable; 
use Prettus\Repository\Traits\TransformableTrait;

class Membro extends Model implements Transformable {
    use TransformableTrait;

    protected $table    = 'membros';

    protected $fillable = [
        'desde',
        'pretencoes_funcoes_id',
        'pretencoes_profissionais_id',
        'pretencoes_cursos_id'
    ]; 
}

Service MembroService.php

<?php

namespace Igreja\Services;

use Igreja\Repositories\MembroRepository; 
use Igreja\Entities\Membro;

class MembroService {
    private $repository;

    public function __construct(MembroRepository $repository)
    {
        $this->repository = $repository;
    }
}

Folder structure

  

    
asked by anonymous 13.12.2016 / 00:37

1 answer

2

In the composer.json file key, include "Igreja\": "app/" :

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\": "app/",
        "Igreja\": "app/"
    }
},

In the line of code type:

php composer dumpautoload
  

Why do you have to put this setting?

The new namespace created Igreja and its corresponding app/ folder must be registered for loading these classes.

Then create a ServiceProvider with the command:

php artisan make:provider IgrejaServiceProvider

within it in method register() put:

public function register()
{
    $this->app->bind(MembroRepository::class, MembroRepositoryEloquent::class);
    // as demais logo abaixo todas as classes
}

Finishing between the folder config/ file app.php add on providers like this:

App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,

App\Providers\IgrejaServiceProvider::class

Note: If you do not want to create ServiceProvider you can put constructor MembroRepositoryEloquent (I believe it is the common class that can be instantiated, so the first part of the solution would solve it already.)

References:

13.12.2016 / 00:53