laravelutilities / repository
带有缓存实现的 Laravel 模型插件
v3.1.0
2024-03-22 08:57 UTC
README
此包允许您轻松创建模型仓库。您可以将此包作为现有 Laravel 模型类的插件使用。以下是亮点:
- 缓存仓库
- 模型仓库
- 制作仓库 artisan 命令
入门指南
1. 安装
运行以下命令
composer require laravelutilities/repository
2. 发布
发布配置文件。
php artisan vendor:publish --tag=repository
4. 配置
您可以通过 config/repository.php
文件更改应用程序的选项
用法
您可以在任何控制器中注入仓库的依赖关系,并使用其函数,如 $this->repository->fetchByCreatedAt('foo')
或如果您想使用缓存 $this->repository->fetchByCreatedAt('foo', 'cache')
变体
$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)
控制器构造函数
use App\Repositories\OrganizationRepository; protected $organization; public function __construct() { $this->organization = new OrganizationRepository(); }
带有数据库的仓库类
namespace App\Repositories; use App\Models\AppLog; use LaravelUtility\Repository\Repositories\ModelRepository; class AppLogRepository extends ModelRepository { public function __construct() { parent::__construct(new AppLog()); } }
带有缓存的仓库类
namespace App\Repositories; use App\Models\AppLog; use LaravelUtility\Repository\Repositories\CacheRepository; class AppLogRepository extends CacheRepository { public function __construct() { parent::__construct(new AppLog()); } }
模型仓库方法
public function findByField($key, $value); public function findByFields(array $fieldsAndValues); public function fetchByField($key, $value); public function fetchByFields(array $fieldsAndValues);
缓存仓库方法
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)
访问器特质
已经添加了一个特质,以使用魔术方法进一步扩展这些功能。在上述所有提到的函数中,Field
可以在任何模型字段中替换。假设您在表中有一个 created_at
字段;可以通过以下方式调用它
$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
制作仓库命令
php artisan make:repository <name> --model=<model> --cache=true/false
您可以使用 make:repository
命令创建仓库类。
<name>
是必填项;指定仓库的名称。使用 Repository
作为后缀是必填项。如果您没有以 Repository
作为后缀写入名称,则命令会为您完成。
--model=modelname
作为选项,如果您没有提到模型的名称,它将尝试找到与仓库相同的模型。如果模型尚未创建,它将询问您是否要创建新模型。
cache=true|false
,默认为 true,您的仓库扩展 CacheRepository 而不是 Model Repository
变更日志
有关最近更改的更多信息,请参阅 发行版
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 LICENSE