【问题标题】:BindingResolutionException Target [Illuminate\Database\Eloquent\Model] is not instantiableBindingResolutionException 目标 [Illuminate\Database\Eloquent\Model] 不可实例化
【发布时间】:2014-11-29 02:22:12
【问题描述】:

我一直在努力解决以下错误。这不是接口错误,而是模型错误。我不知道下面的代码有什么问题。

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 = [];
}

即companyrepository接口

namespace ohbt\Repositories;
 interface CompanyRepositoryInterface{
 }

那是公司仓库

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();
}

}

非常感谢任何建议和帮助。感谢先进。

【问题讨论】:

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


    【解决方案1】:

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

    我在 RepositoryServiceProvider 中描述了具体的文件和模型。但是其他 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 绑定。这不是一个好主意。因此,非常欢迎先进的解决方案或答案,以获得更好的解决方案。

    【讨论】:

    • 现在我面临公司存储库中使用的公司验证器服务的问题。 ` $this->validator->isValid($attributes); ` 我得到“试图获取非对象的属性”错误。请帮助适当的解决方案。为什么这个接口不能像其他接口一样绑定?
    • 您是否将 RepositoriesServiceProvider 添加到 app.php 中的 providers 数组中?
    猜你喜欢
    • 2022-10-16
    • 2021-04-20
    • 2013-08-04
    • 1970-01-01
    • 2021-10-25
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    相关资源
    最近更新 更多