BindingResolutionException目标[Illuminate \ Database \ Eloquent \ Model]不可实例化

时间:2014-10-04 01:57:49

标签: laravel-4 dependency-injection model-binding

我遇到了以下错误。它不是关于接口错误而是模型错误。我不知道下面的代码有什么问题。

Illuminate \ Container \ BindingResolutionException

Target [Illuminate\Database\Eloquent\Model] is not instantiable.

我没有任何想法来解决这个问题。

我有公司模特。

use Illuminate\Database\Eloquent\Model;

class Company extends Eloquent{
   protected $softDelete = true;
   protected $table = "companies";
   protected $fillable = [];
   protected $guarded = [];
}

这是companyrepositoryinterface

namespace ohbt\Repositories;
 interface CompanyRepositoryInterface{
 }

这是companyrepository

namespace ohbt\Repositories\Eloquents;

use Company;
use Illuminate\Database\Eloquent\Model;
use ohnt\Services\Validator\CompanyValidator as Validator;
use ohbt\Repositories\CompanyRepositoryInterface as CompanyRepositoryInterface;

class CompanyRepository extends AbstractRepository implements CompanyRepositoryInterface{

   protected $company;
   protected $validator;

    public function __constructor(Model $company, Validator $validator){
        $this->company = $company;
        parent::__construct($this->company);
        $this->validator = $validator;
    }

    public function create(array $attributes){
    if($this->validator->isValid($attributes)){
        $this->company->create($attributes);
        return true;
    }
    return false;
    }

    public function getMessages(){
       return $this->validator->getMessages();
    }
}

我的存储库服务提供商已添加到app / config / app.php

namespace ohbt\Repositories;
 use Illuminate\Support\ServiceProvider;

class RepositoriesServiceProvider extends ServiceProvider {

protected $defer = false;

public function boot()
{
    //
}

public function register()
{
    $this->app->bind('VehicleRepositoryInterface', 'ohbt\Repositories\Eloquents\VehicleRepository');

    $this->app->bind('UserRepositoryInterface', 'ohbt\Repositories\Eloquents\UserRepository');

    $this->app->bind('CompanyRepositoryInterface', 'ohbt\Repositories\Eloquents\CompanyRepository');
}

public function provides()
{
    return array();
}

}

真的很感激任何建议和帮助。谢谢高级。

1 个答案:

答案 0 :(得分:0)

实际上,我不认为这是模型实例化问题的合适或良好的解决方案。但它解决了我最近的问题。

我在RepossitoryServiceProvider中描述了特定的文件和模型。但是其他Binding工作虽然这个问题没有任何意义。

这是我的解决方案

 use \Company;   //Company Model added 
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\ServiceProvider;
 use ohbt\Repositories\Eloquents\CompanyRepository;  //Company Repository added.

class RepositoriesServiceProvider extends ServiceProvider {

protected $defer = false;

public function boot()
{
    //
}

public function register()
{
    $this->app->bind('ohbt\Repositories\BaseRepositoryInterface', 'ohbt\Repositories\Eloquents\AbstractRepository');

    $this->app->bind('VehicleRepositoryInterface', 'ohbt\Repositories\Eloquents\VehicleRepository');

    $this->app->bind('UserRepositoryInterface', 'ohbt\Repositories\Eloquents\UserRepository');

    $this->app->bind('GuestRepositoryInterface', 'ohbt\Repositories\Eloquents\GuestRepository');

     /**
      * Comment out the previous binding function. 
      * $this->app->bind('CompanyRepositoryInterface',
      * 'ohbt\Repositories\Eloquents\CompanyRepository');
      * directly return Repository Object rather than directory of Repository of Model
      **/
    $this->app->bind('CompanyRepositoryInterface', function()
    {
        return new CompanyRepository( new Company );
    });
}

public function provides()
{
    return array();
}

}

在这种情况下,我们必须声明所有模型和存储库文件与IoC绑定。这不是一个好主意。所以,欣赏地欢迎先进的解决方案或答案,以便更好地接近。