mayconbordin/reloquent
Composer 安装命令:
composer require mayconbordin/reloquent
包简介
Repository data layer for Laravel 5
README 文档
README
Verbose Repository Data Access Layer for Laravel 5.
Yet Another Repository package?
If you search within Github you can find at least a dozen implementations of the repository pattern for Laravel 4 and 5. For Laravel 5, specifically, the best ones are prettus/l5-repository and Bosnadev/Repositories.
The difference between these packages and this one is a more flexible interface, enabling more complex queries through the Repository. And a lot of tests.
Installation
Add
"mayconbordin/reloquent": "dev-master"
to your composer.json. Then run composer install or composer update.
Add the Service Provider to the providers array in config/app.php:
'providers' => array( ... 'Mayconbordin\Reloquent\Providers\ReloquentServiceProvider', )
And then publish the configurations
php artisan vendor:publish
API & Usage
In the following sections examples will be given for all the interface methods available in the repository.
Assume we have a
Postclass model with columns:id,title,content,category_idandauthor_id.
findAllBy
// to find all posts that belong to a single category $repository->findAllByCategoryId(1); // fetch also the post tags and category $repository->findAllByCategoryIdWith(1, ['tags', 'category']); // we can also order the results by id in ascending order $repository->findAllByCategoryIdOrderById(1); // or descending $repository->findAllByCategoryIdOrderByDescId(1); // we could then limit the number of results returned to 15 $repository->findAllByCategoryIdOrderByIdLimit(1, 15); // or even paginate the results, with 15 records per page $repository->findAllByCategoryIdOrderByIdPaginated(1, 15); // we can also get posts within a list of categories $repository->findAllByInCategoryId([1, 2, 3, 4]); // or filter the posts by category and author $repository->findAllByCategoryIdAndAuthorId(1, 2); // or by category or author $repository->findAllByCategoryIdOrAuthorId(1, 2);
findBy
// find a single post by author id $repository->findByAuthorId(1); // or with author id AND category id $repository->findByCategoryIdAndAuthorId(1, 2); // or with author id OR category id $repository->findByCategoryIdOrAuthorId(1, 2); // fetch also the post tags and category objects $repository->findByAuthorIdWith(1, ['tags', 'category']);
create, update and delete
$attributes = [ 'title' => 'Post Title', 'content' => 'Post content...', 'category_id' => 1, 'author_id' => 1 ]; // create a new post $post = $repository->create($attributes); // update the title of the post $updatedPost = $repository->update(['title' => 'Update Post Title'], $post->id); // and delete it $repository->delete($updatedPost->id);
Creating and deleting models with their relations
By default the repository doesn't care about the relations a model might have, which means that you can't give an instance of a related model as an attribute value to the create method. You also can't have the related objects removed before the object in question, and if the database is not configured to remove the children objects (DELETE CASCADE) the operation will fail.
To make the repository aware of the relations, add the attribute relations to the repository implementations, like this:
class PostRepository extends BaseRepository { protected $relations = ['category', 'author']; ... }
Now you can create a new post like this:
$attributes = [ 'title' => 'Post Title', 'content' => 'Post content...', 'category' => Category::find(1), 'author' => Author::find(1) ]; // create a new post $post = $repository->create($attributes);
If a post could also have tags, you could create a post with tags like this:
$attributes = [ 'title' => 'Post Title', 'content' => 'Post content...', 'category' => Category::find(1), 'author' => Author::find(1), 'tags' => [1, 2, 3, 4] ];
BelongsTo relations are saved using the associate method, while BelongsToMany use the attach method. And when a post is deleted, the repository will iterate over the defined relations and remove them. For relations that are instances of HasOneOrMany the method delete is used, and for BelongsToMany the detach method.
exists, find and findByField
// check if a post exists, returns a boolean $repository->exists(1); // find a post by id $repository->find(1); // bring also its relations $repository->find(1, ['author', 'tags']); // find a post by author $repository->findByField('author_id', 1); // find a post whose title starts with 'A' $repository->findByField('title', 'A%', 'LIKE'); // and you can also bring the post's relations $repository->findByField('title', 'A%', 'LIKE', ['author', 'tags']);
findWhere
The findWhere method is not as pretty as the other methods. The first argument is an associative array of where statements.
An statement is an array that contains the value of the field, and optionally the operator and the statement.
$repository->findWhere([ 'title' => ['test'], // WHERE title = 'test' 'category_id' => ['!=', 2], // AND category_id != 2 'author_id' => ['or', 1] // OR author_id = 1 'content' => ['or', 'like', '%test%'] // OR content LIKE %test% ]);
all, findAllByField and findAllWhere
// get all posts $repository->all(); // get all posts ordered by title $repository->all('title:asc'); // get all posts ordered by id and title $repository->all(['id:asc', 'title:asc']); // get all posts by author $repository->findAllByField('author_id', 1); // get all posts not by author $repository->findAllByField('author_id', 1, '!='); // get all posts by author ordered by title $repository->findAllByField('author_id', 1, '=', 'title:asc'); // get 15 posts by author ordered by title $repository->findAllByField('author_id', 1, '=', 'title:asc', ['author'], 15); // same as the above query $repository->findAllWhere(['author_id' => 1], 'title:desc', ['author'], 15);
paginate, findAllByFieldPaginated and findAllWherePaginated
// get all posts $repository->paginate(); // get 15 posts per page ordered by title $repository->paginate(15, 'title:asc'); // get 15 posts per page posts ordered by id and title $repository->paginate(15, ['id:asc', 'title:asc']); // get 15 posts per page posts ordered by id and title with author $repository->paginate(15, ['id:asc', 'title:asc'], ['author']); // get posts by author $repository->findAllByFieldPaginated('author_id', 1); // get posts not by author $repository->findAllByFieldPaginated('author_id', 1, '!='); // get posts by author ordered by title $repository->findAllByFieldPaginated('author_id', 1, '=', 'title:asc'); // get 15 posts per page by author ordered by title $repository->findAllByFieldPaginated('author_id', 1, '=', 'title:asc', 15, ['author']); // same as the above query $repository->findAllWherePaginated(['author_id' => 1], 15, 'title:desc', ['author']);
mayconbordin/reloquent 适用场景与选型建议
mayconbordin/reloquent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 113 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 07 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「repository」 「laravel」 「eloquent」 「laravel 5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mayconbordin/reloquent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mayconbordin/reloquent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mayconbordin/reloquent 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
repository php library
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 113
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-29