gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:57:55 25 4
gpt4 key购买 nike

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

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

并且我的 repositoryservices provider 已经添加到 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();
}

}

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

最佳答案

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

我在 RepossitoryServiceProvider 中描述了具体的文件和模型。但是当这个有问题时其他绑定(bind)工作没有意义。

这是我的解决方案

 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 绑定(bind)。那不是一个好主意。因此,非常欢迎高级解决方案或更好的答案。

关于laravel-4 - BindingResolutionException 目标 [Illuminate\Database\Eloquent\Model] 不可实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26188887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com