dannyweeks / laravel-base-repository
这是一个抽象的仓库类,用于您的Eloquent仓库,只需最少配置即可开始使用。
Requires
- illuminate/database: >4.0
- illuminate/pagination: 5.2.*
Requires (Dev)
- mockery/mockery: ^0.9.4
- orchestra/testbench: 3.2.*
- phpunit/phpunit: ^4.8
Suggests
- illuminate/cache: Required to use the Caching trait (5.2.*).
This package is not auto-updated.
Last update: 2024-09-28 19:09:28 UTC
README
这是一个抽象的仓库类,用于您的Eloquent仓库,只需最少配置即可开始使用。
特性
- 2分钟快速设置。
- 包含诸如
getById
等10个有用的方法。 - 灵活的关系支持,包括预加载。
- 可选的简单缓存。
- 可选的404异常,当项目未找到时。
快速开始
通过Composer安装。
composer require dannyweeks/laravel-base-repository
通过Weeks\Laravel\Repositories\BaseEloquentRepository
扩展您的仓库。
将$model
属性添加到您的仓库中,以便基础仓库知道使用哪个模型。
namespace App\Repositories; class PostRepository extends \Weeks\Laravel\Repositories\BaseEloquentRepository { protected $model = \App\Models\Post::class; }
就这样!让我们测试一下。
$repo = new App\Repositories\PostRepository(); var_dump($repo->getById(1)); // Returns the Post with an ID of 1. var_dump($repo->getAll()); // Returns a collection of all your posts.
用法
您的仓库必须扩展BaseEloquentRepository
类,并具有以下属性
protected $model
:您的模型名称(包括它的命名空间)protected $relationships
:(可选)一个数组,当检索项目时将包含可用的方法。
$posts = new App\Repositories\PostRepository(); $firstPost = $posts->getById(1); $allPosts = $posts->getAll(); $allPostsIncludingComments = $posts->with('comments')->getAll();
请务必查看示例。
可用方法
查看仓库接口类以获取完整的API。
关系
关系在仓库中定义,但不会自动预加载。
可以使用以下三种方式使用with()
方法加载关系
$postRepository->with('all')->getAll();
检索仓库类中定义的所有关系$postRepository->with(['comments', 'author'])->getAll();
使用数组检索关系$postRepository->with('comments')->getAll();
使用字符串检索关系
示例
此示例显示了如何设置您的模型、仓库和控制器。
app\Models\Post.php
namespace App\Models; class Post extends Illuminate\Database\Eloquent\Model { public function comments() { return $this->hasMany('App\Models\Comment'); } public function author() { return $this->hasOne('App\Models\User'); } }
app\Repositories\PostRepository.php
namespace App\Repositories; use Weeks\Laravel\Repositories\BaseEloquentRepository; class PostRepository extends BaseEloquentRepository { protected $model = App\Models\Post::class; protected $relationships = ['comments', 'author']; }
app\Http\Controllers\PostController.php
namespace App\Http\Controllers; use App\Repositories\PostRepository; class PostController extends Controller { protected $posts; public function __construct(PostRepository $posts) { $this->posts = $posts; } public function show($id) { // get the post and eagerly load the comments for it too. $post = $this->posts->with('comments')->getById($id); return view('posts.show', compact('post')); } }
HTTP异常
要在仓库上启用http异常(类似于Eloquent的findOrFail方法),只需在它中使用use \Weeks\Laravel\Repositories\Traits\ThrowsHttpExceptions;
。
如果以下方法返回null,它们将抛出404错误而不是返回null。
getById
getItemByColumn
使用ThrowsHttpExceptions特性示例。
namespace App\Repositories; use Weeks\Laravel\Repositories\BaseEloquentRepository; use Weeks\Laravel\Repositories\Traits\ThrowsHttpExceptions; class PostRepository extends BaseEloquentRepository { use ThrowsHttpExceptions; protected $model = App\Models\Post::class; }
您可以在执行查询之前使用disableHttpExceptions()
链来暂时禁用HTTP异常。例如
$posts = new PostRepository(); $post = $posts->disableHttpExceptions()->getById(1000); // returns null rather than throwing a 404 error.
缓存
要在仓库上启用缓存,只需在它中使用use \Weeks\Laravel\Repositories\Traits\CacheResults;
。
这样做,所有仓库的'read'方法都会使用Laravel的缓存系统缓存它们的结果。
// Methods that cache when using the CacheResults trait.
getAll
getPaginated
getForSelect
getById
getItemByColumn
getCollectionByColumn
getActively
使用CacheResults特性示例。
namespace App\Repositories; use Weeks\Laravel\Repositories\BaseEloquentRepository; use Weeks\Laravel\Repositories\Traits\CacheResults; class PostRepository extends BaseEloquentRepository { use CacheResults; protected $model = App\Models\Post::class; protected $relationships = ['comments', 'author']; protected $nonCacheableMethods = ['getById']; protected $cacheTtl = 30; }
您可以通过将方法名称添加到仓库的$nonCacheableMethods
属性中,强制请求的结果不被缓存。请参见上面的示例。
默认情况下,缓存项的TTL为60分钟。这可以通过更新仓库的$cacheTtl
属性来重写。请参见上面的示例。
可以通过在您的仓库上调用disabledCaching()
来程序性地禁用缓存。此方法将仓库返回到启用方法链状态,例如:$repo->disableCaching()->getAll();
。