【发布时间】: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