我是 php 和 Laravel 的初学者。我在我的项目 Laravel 7 中使用。我的项目中有带有缓存的存储库模式。页面服务提供者:public function register(){ $this->app->bind(PageRepositoryInterface::class, function ($app) { return new CachingPageRepository( new PageRepository ); });}public function provides(){ return [ PageRepositoryInterface::class, ];}缓存库:abstract class CachingBaseRepository implements RepositoryInterface{ use ScopeActiveTrait; protected $model; public function all() { return Cache::remember($this->model.'.all', $minutes = 10, function () { return $this->model->get(); }); } public function allEnables() { return Cache::remember($this->model.'.enables', $minutes = 10, function () { return $this->model->active()->get(); }); } public function list(string $orderByColumn, string $orderBy = 'desc', array $with = []) { return Cache::remember($this->model.'.list', $minutes = 10, function () use($with, $orderByColumn, $orderBy) { return $this->model->with($with) ->orderBy($orderByColumn, $orderBy) ->get(); }); } public function listWithPaginate(string $orderByColumn, string $orderBy = 'desc', array $with = [], int $perPage = 10) { return Cache::remember($this->model.'.listWithPaginate', $minutes = 10, function () use($with, $orderByColumn, $orderBy, $perPage) { return $this->model->with($with) ->orderBy($orderByColumn, $orderBy) ->paginate($perPage)->appends(request()->query()); }); } public function create(array $data): int { return $this->model->create($data)->id; // delete cache: all, enables, list, listWithPaginate }
1 回答
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
从 PageRepository 中的 _construct,你的 $model 是一个页面。构造函数需要模型/页面来实例化一个新的 PageRepository:
public function register()
{
$this->app->bind(PageRepositoryInterface::class, function ($app) {
return new CachingPageRepository(
new PageRepository(new Page()) //constructor for PageRepository needs a model
);
});
}
public function provides()
{
return [
PageRepositoryInterface::class,
];
我把“new Page()”放在那里,但我真的不知道你从哪里得到你的新页面实例。但是,从构造函数中可以清楚地看出,您需要在那里输入一个 Page 实例:
public function __construct(Page $model)
{
$this->model = $model;
}
- 1 回答
- 0 关注
- 80 浏览
添加回答
举报
0/150
提交
取消