【问题标题】:Error in Laravel Too few arguments to functionLaravel 中的错误 参数太少而无法运行
【发布时间】:2020-09-10 13:31:00
【问题描述】:

我是 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,
    ];
}

CachingBaseRepository:

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
    }

    public function update(array $data, int $id, string $attribute = 'id'): void
    {
        $this->model->where($attribute, '=', $id)->update($data);
        // delete cache: all, enables, list, listWithPaginate
    }

    public function delete(int $id): void
    {
        $this->model->destroy($id);
        // delete cache: all, enables, list, listWithPaginate
    }

    public function find(int $id)
    {
        return Cache::remember($this->model.".{$id}", $minutes = 60, function () use ($id) {
            return $this->model->find($id);
        });
    }

    public function getModel()
    {
        return Cache::remember($this->model.".all", $minutes = 60, function (){
            return $this->model;
        });
    }

    public function getFirst(int $id)
    {
        return Cache::remember($this->model.".{$id}", $minutes = 60, function () use ($id) {
            return $this->model->where('id', $id)->first();
        });
    }

    public function findOrFail(int $id)
    {
        return Cache::remember($this->model.".{$id}", $minutes = 60, function () use ($id) {
            return $this->model->findOrFail($id);
        });
    }
}

基础存储库:

abstract class BaseRepository implements RepositoryInterface
{
    use ScopeActiveTrait;

    protected $model;

    public function all()
    {
        return $this->model->get();
    }

    public function allEnables()
    {
        return $this->model->active()->get();
    }

    public function list(string $orderByColumn, string $orderBy = 'desc', array $with = [])
    {
        return $this->model->with($with)
            ->orderBy($orderByColumn, $orderBy)
            ->get();
    }

    public function listWithPaginate(string $orderByColumn, string $orderBy = 'desc', array $with = [], int $perPage = 10)
    {
        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;
    }

    public function update(array $data, int $id, string $attribute = 'id'): void
    {
        $this->model->where($attribute, '=', $id)->update($data);
    }

    public function delete(int $id): void
    {
        $this->model->destroy($id);
    }

    public function find(int $id)
    {
        return $this->model->find($id);
    }

    public function getModel()
    {
        return $this->model;
    }

    public function getFirst(int $id)
    {
        return $this->model->where('id', $id)->first();
    }

    public function findOrFail(int $id)
    {
        return $this->model->findOrFail($id);
    }
}

PageRepository:

class PageRepository extends BaseRepository implements PageRepositoryInterface
{

    public function __construct(Page $model)
    {
        $this->model = $model;
    }


    public function search(string $query, string $orderByColumn, string $orderBy = 'desc', array $with = [], int $perPage = 10)
    {
        return $this->model->where('title', 'LIKE', '%' . $query . '%')->orWhere('description', 'LIKE', '%' . $query . '%')->orWhere('keywords', 'LIKE', '%' . $query . '%')->orWhere('content', 'LIKE', '%' . $query . '%')->with($with)->orderBy($orderByColumn, $orderBy)->paginate($perPage)->appends(request()->query());
    }

    public function getTextPageFromSlug(string $slug)
    {
        return $this->model->active()->where('slug', $slug)->first();
    }

}

CachingPageRepository

class CachingPageRepository extends CachingBaseRepository implements PageRepositoryInterface
{
    public function __construct(Page $model)
    {
        $this->model = $model;
    }


    public function search(string $query, string $orderByColumn, string $orderBy = 'desc', array $with = [], int $perPage = 10)
    {
        return Cache::remember('page.all', $minutes = 10, function () use($query, $orderByColumn, $with, $orderBy, $perPage) {
            return $this->model->where('title', 'LIKE', '%' . $query . '%')->orWhere('description', 'LIKE', '%' . $query . '%')->orWhere('keywords', 'LIKE', '%' . $query . '%')->orWhere('content', 'LIKE', '%' . $query . '%')->with($with)->orderBy($orderByColumn, $orderBy)->paginate($perPage)->appends(request()->query());
        });
    }


    public function getTextPageFromSlug(string $slug)
    {
        return Cache::remember("users.{$slug}", $minutes = 60, function () use ($slug) {
            return $this->model->active()->where('slug', $slug)->first();
        });
    }
}

PageRepositoryInterface:

interface PageRepositoryInterface extends RepositoryInterface
{

    public function search(string $query, string $orderByColumn, string $orderBy = 'desc', array $with = [], int $perPage = 30);


    public function getTextPageFromSlug(string $slug);

}

我想在上面的代码中为我的网站添加缓存。我的控制器如下所示:

protected $model;


    public function __construct(PageRepositoryInterface $repository)
    {
        $this->model = $repository;
    }


    public function index(Request $request)
    {
        if ($request->input('query') != "") {
            $pages = $this->model->search($request->input('query'), 'id', 'asc', [],  30);
        } else {
            $pages = $this->model->listWithPaginate('id', 'desc', [],  30);
        }
        return view('admin.pages.list', ['pages' => $pages]);
    }

当我运行上面的代码时,我得到了错误:

ArgumentCountError 函数参数太少 App\Repositories\PageRepository::__construct(), 0 传入 /var/www/app/Providers/PageServiceProvider.php 在第 22 行,完全正确 预计 1 个

之前,当我在页面上没有缓存时,我的 PageServiceProvider 看起来像这样:

public function register()
    {
        $this->app->bind(
            PageRepositoryInterface::class,
            PageRepository::class
        );
    }

并且代码运行没有问题。

我该如何修复它?

请帮帮我。

【问题讨论】:

    标签: php laravel laravel-5 caching laravel-7


    【解决方案1】:

    从 PageRepository 中的 _construct 中,您的 $model 是一个 Page。构造函数需要模型/页面来实例化一个新的 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;
    }
    

    【讨论】:

    • 非常感谢您的回答。传递给 App\Repositories\Caching\CachingPageRepository::__construct() 的参数 1 必须是 App\Models\Page 的实例,给定 App\Repositories\PageRepository 的实例,在 /var/www/app/Providers/PageServiceProvider.php 中调用在第 23 行```
    • 嗯,该错误消息是不言自明的。您需要确保包含/需要 App\Model\Page 类,并且将该类的实例传递给 PageRespository。
    • 是的,这很有帮助。感谢大家的帮助!!! :)。现在有错误: public function listWithPaginate... { return Cache::remember($this->cacheKey.'.listWithPaginate', $minutes = 10, function () use($with, $orderByColumn, $orderBy, $perPage) { return $this->model->with($with) ->orderBy($orderByColumn, $orderBy) ... }); } in line: return $this->model->with($with) Error: Call to undefined method App\Repositories\PageRepository::with() 为什么它不起作用?
    猜你喜欢
    • 2020-06-06
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 2020-09-02
    • 2013-06-24
    • 1970-01-01
    相关资源
    最近更新 更多