【问题标题】:Can't get the laravel custom repository to work无法让 laravel 自定义存储库工作
【发布时间】:2019-02-01 18:06:21
【问题描述】:

我无法让我的存储库正常工作,当我只是尝试获取完整的文档列表时,它什么也不返回

这是我的文档库

<?php

namespace App\Repositories\Document;

interface DocumentRepository
{
  public function getall();

  public function getById($id);

  public function create(array $attributes);

  public function update ($id, array $attributes);

  public function delete ($id);

}

这里是函数

<?php


namespace App\Repositories\Document;

class EloquentDocument implements DocumentRepository
{

  private $model;

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


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

  public function getById($id)
  {
    return $this->findById($id);
  }

  public function create(array $attributes)
  {
    return $this->model->create($attributes);
  }

  public function delete($id)
  {
    $this->getById($id)->delete();
    return true;
  }

  public function update($id array $attributes)
  {
    $document = $this->model->findOrFail($id);
    $document->update($attribute);
    return $document;
  }
}

这是控制器

<?php
namespace App\Http\Controllers;

use App\Repositories\Document;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DocumentController extends Controller
{
    /**
     * @var DocumentRepository
     */
    private $document;
    /**
     * TodoController constructor.
     */
    public function __construct(DocumentController $document)
    {
        $this->document = $document;
    }
    public function getalldocuments()
    {
        return $this->document->getAll();
    }
}

为了您的信息,我的 Documents 表/模型中有两行数据,所以我只想通过简单地返回来获取它们,但在我的情况下它什么也不返回。 这是路线

Route::get('/documents', 'DocumentController@getalldocuments');

这里是 AppServiceProviders.php 中的注册部分

 public function register()
    {
        $this->app->singleton(DocumentRepository::class, EloquentDocument::class);
    }

【问题讨论】:

    标签: laravel eloquent repository repository-pattern


    【解决方案1】:

    您输入的是DocumentController,而不是您的实际存储库。

    <?php
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use App\Repositories\Document\DocumentRepository; 
    
    class DocumentController extends Controller
    {
        /**
        * @var DocumentRepository
        */
        private $document;
    
        public function __construct(DocumentRepository $document)
        {
            $this->document = $document;
        }
    
        public function getalldocuments()
        {
            return $this->document->getAll();
        }
    }
    

    现在,假设您已将要解析的接口正确绑定到实现的文档存储库,这应该可以工作。

    有关如何将接口绑定到实现的更多信息,请阅读:https://laravel.com/docs/5.7/container#binding-interfaces-to-implementations

    编辑:您的存储库界面存在一些语法问题。您缺少功能:

    <?php
    
    namespace App\Repositories\Document;
    
    interface DocumentRepository
    {
      public function getall();
    
      public function getById($id);
    
      public function create(array $attributes);
    
      public function update($id, array $attributes);
    
      public function delete($id);
    }
    

    编辑 2:您的绑定是正确的。但是,我注意到您没有将 App\Document 模型正确绑定到实现。

    <?php
    
    namespace App\Repositories\Document;
    
    use App\Document; 
    
    class EloquentDocument implements DocumentRepository
    {
    
        private $model;
    
        public function __construct(Document $model)
        {
            $this->model = $model;
        }
    
        //
        //
        //
    }
    

    您需要在顶部添加正确的使用语句。假设您的文档模型位于App\Document,这应该可以工作。

    【讨论】:

    • 我收到类似 dis Class App\Repositories\Document\DocumentRepository 的错误不存在以前的异常语法错误,意外的“更新”(T_STRING),期望函数(T_FUNCTION)或常量(T_CONST)(0 )
    • 您在定义方法时缺少关键字 function。请参阅编辑后的答案。
    • 现在修复它我得到 ReflectionException Class App\Repositories\Document\Document 不存在:(((
    • 您能否编辑您的答案以添加您将接口绑定到实现的服务提供者?
    • 圣母,我成功了。谢谢前辈,我欠你一个 :)))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 2019-09-30
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多