manowartop/base-repository-and-service

此包已被废弃且不再维护。作者建议使用 manowartop/repository-service-laravel-pattern 包。
此包最新版本(dev-master)没有可用的许可信息。

基础仓库和服务结构

dev-master 2020-10-21 09:21 UTC

This package is not auto-updated.

Last update: 2021-06-09 11:20:19 UTC


README

此包为您的 Laravel 服务-仓库模式提供基础仓库和服务

  • 添加仓库层,以便您可以通过仓库进行所有调用,而无需直接使用模型
  • 添加支持 CRUD 和搜索功能的服务层

使用方法

  • 创建模型(例如 Post)
  • 创建仓库 PostRepository extends Manowartop\BaseRepositoryAndService\Repositories\BaseRepository
  • 在 PostRepository 中定义属性 protected $modelClass = Post::class;
  • 创建服务 PostService extends Manowartop\BaseRepositoryAndService\Services\BaseCrudService
  • PostService 中定义属性 protected $repository = PostRepository::class;

这样就完成了。现在您可以使用以下方法集

仓库方法

CRUD

  • getModel(): Model - 获取当前仓库的模型
  • create(array $data): ?Model - 创建新模型
  • createMany(array $data): Illuminate\Support\Collection - 从数组创建多个模型
  • update($keyOrModel, array $data): ?Model - 通过 PK 或模型实例更新模型
  • updateOrCreate(array $attributes, array $data): ?Model - 更新或创建模型
  • delete($keyOrModel): bool - 通过 PK 或实例删除模型
  • deleteMany(array $keysOrModels): void - 删除多个(PK 或模型数组)

查询

  • find($key): ?Model - 通过 PK 查找模型
  • findOrFail($value, ?string $column = null): Model - 通过 PK 查找或失败
  • getAll(array $search = []): Collection - 搜索模型集合
  • getAllPaginated(array $search = [], int $pageSize = 15): LengthAwarePaginator - 搜索分页模型集合
  • findMany(array $attributes): Collection - 根据参数查找所有模型
  • findFirst(array $attributes): ?Model - 根据参数查找第一个模型
  • with(array $with): BaseRepositoryInterface - 设置参数 $with 以指定要查询的模型的关系
  • withCount(array $withCount): BaseRepositoryInterface - 设置参数 $withCount 以指定要查询的模型的关系计数

如果您需要指定查询筛选,只需在您的仓库中重写 protected function getFilteredQuery(array $search = []): Builder

服务方法

  • getAllPaginated(array $search = [], int $pageSize = 15): LengthAwarePaginator - 搜索分页模型集合
  • getAll(array $search = []): Collection - 搜索模型集合
  • findOrFail($value, ?string $column = null): Model - 通过 PK 查找或失败
  • create(array $data): ?Model - 创建新模型
  • createMany(array $data): Illuminate\Support\Collection - 从数组创建多个模型
  • update($keyOrModel, array $data): ?Model - 通过 PK 或模型实例更新模型
  • updateOrCreate(array $attributes, array $data): ?Model - 更新或创建模型
  • delete($keyOrModel): bool - 通过 PK 或实例删除模型
  • deleteMany(array $keysOrModels): void - 删除多个(PK 或模型数组)