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

Asked

Viewed 4,106 times

1

I’m developing a system that when sending data by form is returning me that error:

Bindingresolutionexception in Container.php line 763: Target [Church Repositories Membrorepository] is not instantiable while building [Church Http Controllers Membrocontroller, Church Services Membroservice].

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

Follows 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

Estrutura de pastas

  • I already entered this command: php Artisan app:name Church

  • Elipe as is your configuration of the composer.json, because I am asked: you left the standard and so need to register the namespaces

  • Virgilio, "require": { "php": ">=5.6.4", "Laravel/framework": "5.3.", "prettus/L5-Repository": "2.6", "yajra/Laravel-datatables-oracle": "~6.0", "laravelcollective/html": "~5.0", "lord/laroute" : "2." },

  • I put an answer! take a look

1 answer

2


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

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

In the code line type:

php composer dumpautoload

Why do you have to put this setting?

New needs to be registered namespace servant Igreja and its corresponding folder app/ for the loading of these classes.

After that create a ServiceProvider with the command:

php artisan make:provider IgrejaServiceProvider

within it in the method register() place:

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

finishing inside the folder config/ filing cabinet app.php add on providers thus:

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

App\Providers\IgrejaServiceProvider::class

Observing: If you don’t want to create the ServiceProvider can put in the builder MembroRepositoryEloquent(I believe to be the common class that can be instained, so the first part of the solution would already solve).

References:

  • Thanks, but that parameter already exists, see how this: "autoload": { "classmap": [ "database" ], "psr-4": { "Church ": "app/" } },

  • you executed the command php composer dumpautoload? @felipeldnascimento

  • Yes. The error persists.

  • php composer dumpautoload gives any error in the execution? @felipeldnascimento

  • @felipeldnascimento MembroRepository is a interface or a class abstract???

  • @felipeldnascimento if MembroRepository is a interface or class abstract must inform to framework which class is used as instance I believe to be in the case MembroRepositoryEloquent, made the edit in reply by adding the two cases.

  • 1

    Novic, the problem has been solved by doing what you flw on app/config. Thank you !

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.