Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel存储库模式-关系错误_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel存储库模式-关系错误

Php Laravel存储库模式-关系错误,php,laravel,laravel-5,Php,Laravel,Laravel 5,我是Laravel和php的初学者。我在我的项目中有一个5.8 我在我的项目中扮演家长的角色。 我编写存储库库、接口和控制器 我有以下代码: 基本存储库: 抽象类BaseRepository实现RepositoryInterface { 受保护的$model; 公共函数getAll(字符串$order='id',字符串$by='desc') { 返回$this->model->orderBy($order,$by)->get()->appends(request()->query()); } 公

我是Laravel和php的初学者。我在我的项目中有一个5.8 我在我的项目中扮演家长的角色。 我编写存储库库、接口和控制器

我有以下代码:

  • 基本存储库:
  • 抽象类BaseRepository实现RepositoryInterface
    {
    受保护的$model;
    公共函数getAll(字符串$order='id',字符串$by='desc')
    {
    返回$this->model->orderBy($order,$by)->get()->appends(request()->query());
    }
    公共函数getAllWithPaginate(字符串$order='id',字符串$by='desc',int$perPage=1)
    {
    返回$this->model->orderBy($order,$by)->paginate($perPage)->appends(request()->query());
    }
    公共职能与公共关系
    {
    返回$this->model->with($relations);
    }
    公共函数创建(数组$data)
    {
    返回$this->model->create($data);
    }
    公共函数保存(数组$data):int
    {
    $model=$this->model->create($data);
    返回$model->id;
    }
    公共函数更新(数组$data,int$id)
    {
    返回$this->model->where(“id”、“=”、$id)->update($data);
    }
    公共函数删除(int$id)
    {
    返回$this->model->destroy($id);
    }
    公共函数find(int$id、string$order、string$by)
    {
    返回$this->model->find($id)->orderBy($order,$by);
    }
    公共函数findOrFail(int$id、string$order、string$by)
    {
    返回$this->model->findOrFail($id)->orderBy($order,$by);
    }
    公共函数getModel()
    {
    返回$this->model;
    }
    }
    
  • RepositoryInterface:
  • 接口存储接口
    {
    公共函数getAll(字符串$order,字符串$by);
    公共函数getAllWithPaginate(字符串$order、字符串$by、整型$perPage);
    公共函数创建(数组$data);
    公共函数保存(数组$数据);
    公共函数更新(数组$data,int$id);
    公共功能删除(int$id);
    公共函数find(int$id、string$order、string$by);
    公共函数findOrFail(int$id,string$order,string$by);
    公共函数getModel();
    }
    
    3肾上腺素:

    类AdRepository扩展了BaseRepository
    {
    公共功能构造(Ad$模型)
    {
    $this->model=$model;
    }
    公共函数搜索(字符串$query,字符串$order='id',字符串$by='desc',int$perPage=1)
    {
    返回$this->model->where('title','LIKE','%%.$query'.%')->或where('id','LIKE','%%.$query'.%')->或where('content','LIKE','%%.$query'.$query'.%')->orderBy($order,$by)->分页($perPage)->附录(request()->查询());
    }
    }
    
  • 控制器:
  • 使用App\Models\Ad;
    使用\Http\Request;
    使用App\Http\Controllers\Controller;
    使用App\Repositories\AdRepository;
    公共功能索引(请求$Request)
    {
    如果($request->input('query')!=“”){
    $adsList=$this->repository->with('author')->search($request->input('query');
    }否则{
    $adsList=$this->repository->with('author')->getAllWithPaginate();
    }
    返回视图('modules.ad.ad_list',['adsList'=>$adsList]);
    }
    受保护的$model;
    公共功能构造(AdRepository$存储库)
    {
    $this->model=$repository;
    }
    
    运行代码时出现错误:调用undefined方法Illumb\Database\Eloquent\Builder::getAllWithPaginate()

    我怎样才能修好它

    请帮帮我。

    试试看

    public function with($relations)
    {
        $this->model->with($relations);
        return $this;
    }
    
    试一试


    您能否显示控制器的
    \u结构
    ?受保护的$model;公共函数uu构造(AdRepository$repository){$this->model=$repository;}并添加请
    use
    sectionuse-App\Models\Ad;使用\Http\Request;使用App\Http\Controllers\Controller;使用App\Repositories\AdRepository;请将其添加到您的问题中-不要在注释中扫描您显示的控制器受保护的$model的
    \u构造
    ;公共函数uu构造(AdRepository$repository){$this->model=$repository;}并添加请
    use
    sectionuse-App\Models\Ad;使用\Http\Request;使用App\Http\Controllers\Controller;使用App\Repositories\AdRepository;请将其添加到您的问题中-不在注释中此返回:方法Illumb\Database\Eloquent\Collection::with不存在。我已更改了answerNow我的代码运行,但它不返回带关系的值。它是仅返回值(不带关系)此返回:方法Illumb\Database\Eloquent\Collection::with不存在。我已更改了answerNow我的代码运行,但它不是带关系的返回值。它只返回值(没有关系)