vydev/easy-repository
Composer 安装命令:
composer require vydev/easy-repository
包简介
Laravel Repositories is a package for Laravel 5 or higher which is used to abstract the database layer. This makes applications much easier to maintain.
README 文档
README
Laravel Repositories is a package for Laravel 5 which is used to abstract the database layer. This makes applications much easier to maintain.
Installation
https://packagist.org/packages/vydev/easy-repository
Run the following command from you terminal:
$ composer require vydev/easy-repository
Usage
In config/app.php
Add VyDev\Providers\RepositoryServiceProvider::class
/* * Package Service Providers... */ VyDev\Providers\RepositoryServiceProvider::class
Then run command
php artisan vendor:publish --provider "VyDev\Providers\RepositoryServiceProvider"
Edit config/repositories.php if you want to change Repository store path
Run command to generate repository file
Create a new repository
$ php artisan make:repository UserRepository
Then you can enter the name of Model to automatic generate or skip by enter @
If console throw error Command "make:repository" is not defined."
In : app/Console/Kernel.php
use VyDev\Commands\MakeCriteria; use VyDev\Commands\MakeRepository; protected $commands = [ MakeCriteria::class, MakeRepository::class ];
Then run php artisan optimize:clear
<?php namespace App\Repositories\Eloquent; use VyDev\Repositories\Eloquent\BaseRepository; use VyDev\Repositories\Criteria\AdvancedSearchCriteria; class UserRepository extends BaseRepository { protected $transform = false; public function model() { return 'App\\User'; } public function boot() { /* Uncomment if you want to use advanced search */ // $this->pushCriteria(new AdvancedSearchCriteria()); } public function transform($model) { return [ 'id' => [ 'field' => 'id', 'value' => $model->id, 'visible' => true ] // ]; } /* You can define your customize function bellow */ public function countPosts() { return $this->withCount('posts'); } }
Demo : App\Https\Controllers\User\UserController
<?php namespace App\Http\Controllers\User; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Repositories\Eloquent\UserRepository as User; class IndexController extends Controller { protected $user; public function __construct(User $user) { $this->user = $user; } public function index() { $users = $this->user->all()->export(); $firstUserPosts = $this->user->with('posts')->get()->export(); /* If you want set cache in range of a time */ $cacheKey = 'first-user-posts'; $time = 10; $firstUserPosts = $this->user->with('posts')->get()->exportWithCache($cacheKey,$time); /* Set cache forever */ $firstUserPosts = $this->user->with('posts')->get()->exportWithCache($cacheKey); // Set only cache key } }
Repository Interface
public function all(); public function get($columns = ['*']); public function first(); public function latest(); public function random(); public function exists(); public function find($id); public function findOrFail($id); public function findOrNew($id, $columns = ['*']); public function pluck($columns,$key); public function sync($attributes); public function syncWithoutDetaching($attributes); public function attach($attributes); public function detach($attributes); public function count(); public function firstOrNew(array $attributes); public function firstOrCreate(array $attributes); public function limit($arg); public function take($arg); public function offset($arg); public function paginate($limit = 15, $columns = ['*']); public function where($field,$operator, $value = null); public function whereIn($field,$values); public function whereNotIn($field,$values); public function whereBetween($field,$values); public function create($values); public function save(); public function update($values); public function delete(); public function updateOrCreate(array $attributes, array $values = []); public function has($relation); public function with($relations); public function withCount($relations); public function loadCount($relations); public function whereHas($relation, $closure); public function orderBy($column, $direction = 'asc'); public function load($relation); public function search($fields,$value); public function hidden($columns = ['*']); public function visible($columns = ['*']); public function increment($field, $quantity = null); public function decrement($field, $quantity = null); public function export();
Create a new criteria
$ php artisan make:criteria User/UserActive
<?php namespace App\Repositories\Criteria\User; use VyDev\Repositories\Contracts\RepositoryInterface; use VyDev\Repositories\Criteria\Criteria; class UserActive extends Criteria { public function apply($model, RepositoryInterface $repository) { $model = $model->where('active',1); return $model; } }
To use Criteria
namespace App\Http\Controllers\User; use App\Repositories\Criteria\UserActive; public function testPushCriteria() { /* Uncomment to view the change ^^ */ $this->user->pushCriteria(new UserActive()); // return $this->user->all(); $this->user->skipCriteria(); // return $this->user->all(); } public function testGetByCriteria() { /* If you only want to load a specific Criteria, let do this */ return $this->user->getByCriteria(new UserActive()); } public function testRemoveCriteria() { /* If you have 2 or more criteria and you want to remove one of theme */ $this->user->pushManyCriterias(new UserActive(),new UserHaveFivePosts()); /* Delete a specific criteria */ $this->user->popCriteria(new UserHaveFivePosts()); return $this->user->all()->export(); } public function pushWithCondition(Request $request) { $condition = $request->status == 'active' ? true : false; $this->user->pushCriteriaWhen([ $condition => new FilterActiveUser(), // Somethings else ]); }
Criteria Interface
public function applyCriteria(); public function pushCriteria(Criteria $criteria); public function pushCriteriaWhen($arguments); public function pushManyCriterias(...$criterias); public function skipCriteria(); public function getByCriteria(Criteria $criteria); public function popCriteria(Criteria $criteria); public function pushCriteriaWhen($arguments); public function popManyCriterias(...$criterias);
Transform
In App\Repositories\Eloquent\UserRepository
protected $transform = true; // Define true to use transfomer public function transform($model) { return [ 'id' => [ 'field' => 'id', // The id field in users table 'value' => $model->id // The new data for id field, Example : value => $model->id + 10, 'visible' => true // Show or hidden, if you delete this key, default value is true ] // Some... ]; }
Global Criteria
To use global criteria or somethings else
use VyDev\Repositories\Criteria\AdvancedSearchCriteria; public function boot() { $this->pushCriteria(new AdvancedSearchCriteria()); }
Request all data without filter by request
https://yourdomain.example/products
[
{
id: 1,
cate_id: 2,
producer_id: 3,
name: "TV Sam Sung",
price: 2200,
},
{
id: 2,
cate_id: 1,
producer_id: 3,
name: "Air Conditioner Sony",
price: 3600,
},
{
id: 3,
cate_id: 1,
producer_id: 3,
name: "Air Conditioner Sam Sung",
price: 3650,
},
{
id: 4,
cate_id: 7,
producer_id: 5,
name: "Smart Phone LG",
price: 1200,
}
]
Try it :
https://yourdomain.example/products?search=name:LIKE:Sam%20Sung
[
{
id: 1,
cate_id: 2,
producer_id: 3,
name: "TV Sam Sung",
price: 2200,
},
{
id: 3,
cate_id: 1,
producer_id: 3,
name: "Air Conditioner Sam Sung",
price: 3650,
}
]
https://yourdomain.example/products?search=name:LIKE:Sam%20Sung;id:=:3
[
{
id: 3,
cate_id: 1,
producer_id: 3,
name: "Air Conditioner Sam Sung",
price: 3650,
}
]
https://yourdomain.example/products?search=name:LIKE:Sam%20Sung;id:=:3&filter=name
[
{
name: "Air Conditioner Sam Sung",
}
]
Load a relation and filter ( .* to get all data )
https://yourdomain.example/products?search=name:LIKE:Sam%20Sung;id:=:3&filter=name;producer.id,name
[
{
name: "Air Conditioner Sam Sung",
producer_id: 3,
producer: {
id: 3,
name: "Sam Sung"
}
}
]
Credits
This package was created by Lê Quang Vỹ
------------------- Contact ----------------------
Facebook : https://www.facebook.com/sven307
Email : lequangvy2k@gmail.com
vydev/easy-repository 适用场景与选型建议
vydev/easy-repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 206 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Laravel Repository」 「Easy Repository」 「VyDev Repository」 「Repository Parttern」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vydev/easy-repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vydev/easy-repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vydev/easy-repository 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Secure payment processing via PayU MEA for Magento 2.3
Easy, flexible and lightweight news extension. No extra tables needed, using pages for news articles.
使用简单实用的语义化接口快速读写Excel文件
Laravel 5 - Repositories to the database layer
An easy way for using json encoded strings in php
统计信息
- 总下载量: 206
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-08