ysm/laravel-repository-pattern
Composer 安装命令:
composer require ysm/laravel-repository-pattern
包简介
A Laravel package implementing the Repository Design Pattern with Service Layer
README 文档
README
A lightweight Laravel package implementing the Repository Design Pattern with a Service Layer for clean database operations, supporting Eloquent relationships, sorting, and optional soft deletes. Compatible with Laravel 8.0 to 12.0 and PHP 8.0+.
Installation
-
Install via Composer:
composer require ysm/laravel-repository-pattern
-
Publish configuration:
php artisan vendor:publish --tag=config
Configuration
Edit config/repository-pattern.php to manually define model-to-repository bindings:
return [ 'bindings' => [ \App\Models\User::class => \App\Repositories\UserRepository::class, ], ];
Artisan Commands
-
Generate a repository:
php artisan ysm:repository ModelName [--soft-deletes]
-
Generate a service:
php artisan ysm:service ModelName [--soft-deletes]
-
Generate a controller:
php artisan ysm:controller ModelName [--type=api|web] [--dir=CustomDir] [--soft-deletes] [--with-routes] [--with-rs] --type=api|web: Specify controller type (default: api) --dir=CustomDir: Custom controller and route directory (e.g., Api\Admin\v1) --soft-deletes: Include soft delete methods --with-routes: Generate and bind route file --with-rs: Generate repository and service
Methods
RepositoryInterface / BaseRepository
getAll(array $relations = [], string $orderBy = 'created_at', string $orderDir = 'DESC')- Get all records with optional relationships and sorting.
paginate(int $perPage = 15, array $relations = [], string $orderBy = 'created_at', string $orderDir = 'DESC')- Get paginated records with optional relationships and sorting.
show($value, string $column = 'id')- Get a record by value and column.
create(array $data)- Create a new record.
update($id, array $data)- Update a record by ID.
delete($id)- Delete a record by ID (hard delete).
with(array $relations)- Load relationships for the query.
attach($id, string $relation, array $relatedIds)- Attach IDs to a many-to-many relationship.
detach($id, string $relation, array $relatedIds)- Detach IDs from a many-to-many relationship.
sync($id, string $relation, array $relatedIds)- Sync IDs for a many-to-many relationship.
whereHas(string $relation, Closure $callback)- Query records based on a relationship.
BaseService
- Mirrors all
RepositoryInterfacemethods, delegating to the repository. getRepository()- Get the underlying repository.
InteractsRepoWithSoftDeletes (Repository Trait)
softDelete($id)- Soft delete a record by ID.
permanentlyDelete($id)- Permanently delete a soft-deleted record.
restore($id)- Restore a soft-deleted record.
InteractsServiceWithSoftDeletes (Service Trait)
softDelete($id)- Soft delete a record via repository.
permanentlyDelete($id)- Permanently delete a soft-deleted record via repository.
restore($id)- Restore a soft-deleted record via repository.
Notes
- Compatibility: Requires Laravel 8.0–12.0 and PHP 8.0+. Ensure your project meets these requirements.
- Bindings: Model-to-repository bindings must be manually added to
config/repository-pattern.php. No default binding exists for unbound models; consider adding a default repository or exception. - Controller Directory: Use
--dir=CustomDirto place controllers inApp\Http\Controllers\CustomDir( e.g.,Api\Admin\v1) and routes inroutes/CustomDir/. - Route Generation: The
--with-routesflag forysm:controllergenerates a route file inroutes/api/,routes/web/, orroutes/CustomDir/(e.g.,routes/Api/Admin/v1/users.php) and binds it inRouteServiceProvider. Outputs “🎉 Routes created successfully!”. - Repository and Service Generation: The
--with-rsflag forysm:controllergenerates corresponding repository and service files, respecting--soft-deletes. - Controller Overwrite: The
ysm:controllercommand overwrites existing controllers, displaying “🎉 Controller [Name] (type) overwritten successfully.” or “🎉 Controller [Name] (type) created successfully.” - Repository Overwrite: The
ysm:repositorycommand overwrites existing repositories, displaying “Repository [Name] overwritten successfully.” or “Repository [Name] created successfully.”
License
MIT
ysm/laravel-repository-pattern 适用场景与选型建议
ysm/laravel-repository-pattern 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 8, 最近一次更新时间为 2025 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「php」 「repository」 「command」 「laravel」 「traits」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ysm/laravel-repository-pattern 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ysm/laravel-repository-pattern 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ysm/laravel-repository-pattern 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
repository php library
Laravel 5 - Repositories to the database layer
Structure for the Laravel Service-Repository Pattern
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
Base Skeleton for Laravel Application
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-19