manowartop/repository-service-laravel-pattern

Laravel 服务-仓库模式的结构

v1.8 2023-08-11 13:53 UTC

This package is auto-updated.

Last update: 2024-09-11 16:27:23 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或模型数组)