我是 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條經驗 獲得超8個贊
從 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 關注
- 99 瀏覽
添加回答
舉報
0/150
提交
取消