laravelutilities/repository
Composer 安装命令:
composer require laravelutilities/repository
包简介
Add-on on Laravel Models with caching implementation
README 文档
README
This package allows you to create Model Repository with ease. You can use this package as an add-on on existing laravel model classes. Hightlights are given below
- Cache Repository
- Model Repository
- Make Repository Artisan Commands
Getting Started
1. Install
Run the following command:
composer require laravelutilities/repository
2. Publish
Publish config file.
php artisan vendor:publish --tag=repository
4. Configure
You can change the options of your app from config/repository.php file
Usage
You can inject the dependency of a repository in any controller and use its functions like $this->repository->fetchByCreatedAt('foo') or if you want to use cache $this->repository->fetchByCreatedAt('foo', 'cache')
Variations
$this->repository->findByFieldFromCache('created_at', 'somevalue'); // extending cache repository; fetch only single value $this->repository->findByFieldsFromCache(['created_at' => 'somevalue', 'city' => 'someanothervalue',...]); // fetch multiple values $this->repository->findByField('created_at', 'somevalue'); // extending model repository; fetch only single value $this->repository->findByField('created_at', 'somevalue'); // extending model repository; fetch only single value public function fetchByFieldFromCache($key, $value); public function fetchByFieldsFromCache(array $fieldsAndValues); public function getById($id); public function getByIds(array $ids)
Controller Constructor
use App\Repositories\OrganizationRepository; protected $organization; public function __construct() { $this->organization = new OrganizationRepository(); }
Repository Class with Database
namespace App\Repositories; use App\Models\AppLog; use LaravelUtility\Repository\Repositories\ModelRepository; class AppLogRepository extends ModelRepository { public function __construct() { parent::__construct(new AppLog()); } }
Repository Class with Cache
namespace App\Repositories; use App\Models\AppLog; use LaravelUtility\Repository\Repositories\CacheRepository; class AppLogRepository extends CacheRepository { public function __construct() { parent::__construct(new AppLog()); } }
Model Repository Methods
public function findByField($key, $value); public function findByFields(array $fieldsAndValues); public function fetchByField($key, $value); public function fetchByFields(array $fieldsAndValues);
Cache Repository Methods
public function findByFieldFromCache($key, $value); public function findByFieldsFromCache(array $fieldsAndValues); public function fetchByFieldFromCache($key, $value); public function fetchByFieldsFromCache(array $fieldsAndValues); public function getById($id); public function getByIds(array $ids)
Accessor Trait
A Trait has been added to further augment these functions using magic method. In all the above mentioned functions, Field can be replaced in any of the model field. Suppose, you have a field created_at inside your table; this can be called in variaous ways given below
$this->repository->findByCreatedAt('somevalue'); // extending model repository; fetch only single value $this->repository->findByCreatedAt('somevalue','cache'); // extending cache repository; fetch only single value $this->repository->fetchByCreatedAt(['created_at' => 'somevalue', 'city' => 'someanothervalue',...]); // fetch multiple values
Make Repository Command
php artisan make:repository <name> --model=<model> --cache=true/false
You can create Repository classes using make:repository command.
<name> is mandatory; specifies the name of the repository. Using Repository as a suffix is mandatory. And if you don't write the name with Repository as suffix, command do for you.
--model=modelname as option if you don't menthion the name of the model, it tries to find the model same as Repository .if model is not created it ask if you want to creat new model.
cache=true|false, default is true, your repository extends CacheRepository else Model Repository
Changelog
Please see Releases for more information what has changed recently.
License
The MIT License (MIT). Please see LICENSE for more information.
laravelutilities/repository 适用场景与选型建议
laravelutilities/repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.4k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 laravelutilities/repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laravelutilities/repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-26