当前位置: 首页 > 知识库问答 >
问题:

错误Laravel参数太少无法运行

危彬彬
2023-03-14

我是PHP和Laravel的初学者。我在我的项目Laravel 7中使用。我有存储库模式在我的项目与缓存。

PageServiceProvider:

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

BaseRepository:

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

}

恶作剧记录片

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

当我运行上述代码时,我得到一个错误:

ArgumentCounter函数App\Repositories\PageRepository::u construct(),0传入/var/www/App/Providers/PageServiceProvider的参数太少。php位于第22行,预期正好为1

早些时候,当我在页面上没有缓存时,我的PageServiceProvider如下所示:

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

代码运行没有问题。

我怎样才能修理它?

请帮帮我。

共有1个答案

欧奇希
2023-03-14

从PageRepository中的_construct,您的$模型是一个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,
    ];

我把新页面()放在那里,但我真的不知道你从哪里得到新的页面实例。但是,从构造函数中可以清楚地看出,您需要在那里输入Page的实例:

public function __construct(Page $model) 
{
    $this->model = $model;
}
 类似资料:
  • 我知道有一些与此相关的问题,但是有c或其他语言。我得到了这个错误,我不确定我的功能出了什么问题。这是我的错误 致命错误:Uncaught ArgumentCounter错误:函数Admincategory::deletecategory()的参数太少,在F:\xampp\htdocs\digikalamvc\core\app中传递了0。php位于第34行,在F:\xampp\htdocs\digi

  • 问题内容: 我有这个控制器: 使用此处理程序: 并以这种方式执行: 问题是编译好了。它没有显示任何错误,但是当我转到网站时,仅指向localhost,它显示: 我找不到任何错误,而且编译还可以,这很奇怪…我是Go语言的新手,所以我不知道发生了什么… 问题答案: 我通过阅读以下内容找到了答案: http://codingdict.com/questions/63176 因此,与其尝试调用该方法,不如

  • 我得到以下错误: 致命错误:Uncaught ArgumentCounter错误:参数太少,无法使用wpdb::prepare()函数,第635行的/home/admin/public_html/ocarinatab.com/wp-content/plugins/ocarina-tabs/ocarina-tabs.php中传递了1个,在/home/admin/public_html/ocarina

  • 我正在尝试为用户设置策略。然而,我不断得到一个错误: 太少的参数功能应用\政策\用户政策::更新(),1传递 /vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php在第481行,正好2预期(视图: /resources/views/users/index.blade.php) 错误异常 /app/Policies/UserPoli

  • 刀片: 类AuthServiceProvider扩展了ServiceProvider: 它给我这个错误: 函数App\Providers\AuthServiceProvider::App\Providers{closure}(),在/var/www/html/vendor/laravel/framework/src/light/Auth/Access/Gate中传递的参数太少。第452行和第3行的

  • 当我的WooCommerce购物车页面为空时,我会在页面上出现此错误,我如何消除此错误消息? 致命错误:未捕获的ArgumentCountError:参数太少,无法执行wc_get_page_id(),0在第30行 /home/s3morder/public_html/wp-content/themes/Intranet主题/伍兹商业/购物车/cart-empty.php中传递,而在 /home/