我的控制器是这样的:
...
use App\Repositories\NewsRepository;
class NewsController extends Controller
{
protected $repository;
public function __construct(NewsRepository $repository)
{
$this->repository = $repository;
}
public function store(NewsCreateRequest $request)
{
...
$news = $this->repository->create($input);
...
}
}
我的界面是这样的:
namespace App\Repositories;
use Prettus\Repository\Contracts\RepositoryInterface;
interface NewsRepository extends RepositoryInterface
{
//
}
我的班级是这样的:
...
use App\Repositories\NewsRepository;
class NewsRepositoryEloquent extends BaseRepository implements NewsRepository
{
public function model()
{
return News::class;
}
public function boot()
{
$this->pushCriteria(app(RequestCriteria::class));
}
}
我的存储库服务提供商是这样的:
...
class RepositoryServiceProvider extends ServiceProvider
{
...
public function register()
{
...
$this->app->bind(\App\Repositories\NewsRepository::class, \App\Repositories\NewsRepositoryEloquent::class);
}
}
如果执行控制器上的方法存储,则存在错误:
(1/1)BindingResolutionException目标 构建时,[App \ Repositories \ NewsRepository]不可实例化 [应用\ HTTP \控制器\会员\ NewsRepository]。
如何解决错误?
答案 0 :(得分:0)
您必须将构造函数添加到您的存储库中,并像这样为设置模型调用父构造函数:
public function __construct(Permission $model)
{
parent::__construct($model);
}