manowartop / repository-service-laravel-pattern
Laravel 服务-仓库模式的结构
v1.8
2023-08-11 13:53 UTC
Requires
- php: >=7.2
- illuminate/database: >=5.0
- illuminate/support: >=5.0
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或模型数组)