vydev / repository
Laravel Repositories 是一个用于 Laravel 5 或更高版本的包,用于抽象数据库层。这使得应用程序的维护变得更加容易。
Requires
- php: ^7.1.3
This package is auto-updated.
Last update: 2024-09-04 14:27:06 UTC
README
Laravel Repositories 是一个用于 Laravel 5 的包,用于抽象数据库层。这使得应用程序的维护变得更加容易。
安装
https://packagist.org.cn/packages/vydev/easy-repository
从您终端运行以下命令
$ composer require vydev/easy-repository
用法
在 config/app.php
添加 VyDev\Providers\RepositoryServiceProvider::class
/* * Package Service Providers... */ VyDev\Providers\RepositoryServiceProvider::class
然后运行命令
php artisan vendor:publish --provider "VyDev\Providers\RepositoryServiceProvider"
如果您想更改存储路径,请编辑 config/repositories.php
运行命令以生成存储库文件
创建一个新的存储库
$ php artisan make:repository UserRepository
然后您可以输入模型名称以自动生成,或者通过按@跳过
如果控制台抛出错误 命令 "make:repository" 未定义。
在 app/Console/Kernel.php
use VyDev\Commands\MakeCriteria; use VyDev\Commands\MakeRepository; protected $commands = [ MakeCriteria::class, MakeRepository::class ];
然后运行 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'); } }
演示: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 } }
存储库接口
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();
创建一个新的标准
$ 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; } }
使用标准
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 ]); }
标准接口
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);
转换
在 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... ]; }
全局标准
要使用全局标准或其他内容
use VyDev\Repositories\Criteria\AdvancedSearchCriteria; public function boot() { $this->pushCriteria(new AdvancedSearchCriteria()); }
不按请求过滤请求所有数据
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, } ]
试试
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",
}
]
加载关系并过滤( .* 获取所有数据 )
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" } } ]
鸣谢
此包由 Lê Quang Vỹ 创建
------------------- 联系信息 ----------------------
Facebook : https://#/sven307
Email : lequangvy2k@gmail.com