无法使 laravel 自定义存储库工作
Can't get the laravel custom repository to work
我无法让我的存储库工作,当我只是尝试获取整个文档列表时,它 returns 什么都没有
这是我的 DocumentRepository
<?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();
}
}
供您参考,我的文档 table/model 中有两行数据,所以我只想通过简单地返回来获取它们,但在我的例子中它只是 returns 什么都没有。
这是路线
Route::get('/documents', 'DocumentController@getalldocuments');
这是现场的注册部分 AppServiceProviders.php
public function register()
{
$this->app->singleton(DocumentRepository::class, EloquentDocument::class);
}
您是在类型提示 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
中,这应该可行。
我无法让我的存储库工作,当我只是尝试获取整个文档列表时,它 returns 什么都没有
这是我的 DocumentRepository
<?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();
}
}
供您参考,我的文档 table/model 中有两行数据,所以我只想通过简单地返回来获取它们,但在我的例子中它只是 returns 什么都没有。 这是路线
Route::get('/documents', 'DocumentController@getalldocuments');
这是现场的注册部分 AppServiceProviders.php
public function register()
{
$this->app->singleton(DocumentRepository::class, EloquentDocument::class);
}
您是在类型提示 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
中,这应该可行。