guardian360/repository
Composer 安装命令:
composer require guardian360/repository
包简介
Repository pattern used to abstract the database layer.
README 文档
README
A base repository for Eloquent models.
Requirements
- PHP >=7.0.0
- PHP MongoDB driver (optional)
Installation
Install via composer.
$ composer require guardian360/repository
Usage
First, you should create your repository class. You can do this manually or by
using Artisan by doing $ php artisan make:repository UserRepository. Your
repository must extend \Guardian360\Repository\AbstractRepository and
implement the model() method.
<?php namespace App\Repositories; use App\User; use Guardian360\Repository\AbstractRepository as Repository; class UserRepository extends Repository { /** * Specify the model's class name. * * @return string */ public function model() { return User::class; } }
By implementing the model() method, you are telling the repository what model
class you want to use. Next, in our case, it seems only logical to have a User
model. Why don't we create one?
<?php namespace App; use Illuminate\Database\Eloquent\Model class User extends Model { // }
Finally, you may use the repository in your controller, or anywhere else really.
<?php namespace App\Http\Controllers; use App\Repositories\UserRepository; class UserController extends Controller { /** * @var \App\Repositories\UserRepository */ protected $users; /** * @return void */ public function __construct(UserRepository $users) { $this->users = $users; } /** * Display a listing of the users. * * @return \Illuminate\Http\Response */ public function index() { return response()->json($this->users->all()); } }
Examples
Fetch a list of all users.
$this->users->all();
Find a user.
$this->users->find(1);
Find a user by an attribute.
$this->users->findBy('name', 'Joe');
Find all users by an attribute.
$this->users->findAllBy('role', 'admin'); $this->users->findAllBy('role', ['admin', 'guest']);
Create a new user.
$this->users->create($request->all());
Update an existing user.
$this->users->update($user, $request->all()); $this->users->update(1, $request->all());
Delete an existing user.
$this->users->delete($user); $this->users->delete(1); $this->users->delete([1, 2]);
Specifications
Specifications allow you to apply very specific conditions to the repository's
query. You may use Artisan to generate a Specification class for you, using
$ php artisan make:specification UserIsAdmin, or you may do so manually.
Your specification must satisfy the
\Guardian360\Repository\Contracts\SpecificationContract contract.
<?php namespace App\Specifications; use Guardian360\Repository\Contracts\SpecificationContract as Specification class UserIsAdmin implements Specification { /** * Apply the specification. * * @param mixed $query * @return mixed */ public function apply($query) { return $query->where('role', 'admin'); } }
Then, you may push the Specification to the repository in your controller. You may use as many Specifications as you like.
<?php namespace App\Http\Controllers; use App\Specifications\UserIsAdmin; use App\Repositories\UserRepository; class UserController extends Controller { /** * @var \App\Repositories\UserRepository */ protected $users; /** * @return void */ public function __construct(UserRepository $user) { $this->users = $users; } /** * Display a listing of the admins. * * @return \Illuminate\Http\Response */ public function index() { $admins = $this->users->pushSpec(new UserIsAdmin)->all(); return response()->json($admins); } }
Credits
This package is inspired by the following awesome packages:
- https://github.com/bosnadev/repository
- https://github.com/andersao/l5-repository
- https://github.com/ollieread/laravel-repositories
Thanks guys, I could not have done this without you. :)
guardian360/repository 适用场景与选型建议
guardian360/repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.21k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 10 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 guardian360/repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 guardian360/repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 17.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-17