laravelplus/repository-pattern
Composer 安装命令:
composer require laravelplus/repository-pattern
包简介
Laravel repository-pattern
README 文档
README
A Laravel package that helps you organize all your Eloquent queries for each model in dedicated repository classes, making your codebase more maintainable, testable, and clean. Centralize your data access logic in one spot for each model, following best practices for modern Laravel development.
This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.
Installation
You can install the package via composer:
composer require laravelplus/repository-pattern
Usage
// Usage description here
Repository Pattern Usage
The repository pattern helps you keep all your Eloquent queries for a model in one place, making your codebase more organized and easier to maintain. With this package, you can create repositories in app/Repositories and inject them wherever you need.
Example: app/Repositories/UserRepository.php
namespace App\Repositories; use App\Models\User; class UserRepository { public function all() { return User::all(); } public function find($id) { return User::find($id); } public function create(array $data) { return User::create($data); } // Add more query methods as needed }
Using the Repository in a Controller
use App\Repositories\UserRepository; class UserController extends Controller { protected $users; public function __construct(UserRepository $users) { $this->users = $users; } public function index() { $users = $this->users->all(); return view('users.index', compact('users')); } }
Tip: Place all your model queries in their respective repositories under
app/Repositoriesto keep your code clean and maintainable.
Testing
composer test
Code Style: Pint
This package uses Laravel Pint for code style fixing. To automatically fix code style issues, run:
composer pint
Static Analysis: PHPStan
This package uses PHPStan for static analysis. To run PHPStan, use:
composer phpstan
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email info@after.si instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
Modern Repository Example with Traits
You can enhance your repositories by mixing in traits for extra features like soft deletes, caching, and multi-database support.
Example: app/Repositories/UserRepository.php
namespace App\Repositories; use App\Models\User; use Laravelplus\RepositoryPattern\BaseRepository; use Laravelplus\RepositoryPattern\Traits\SoftDeletes; use Laravelplus\RepositoryPattern\Traits\Cacheable; use Laravelplus\RepositoryPattern\Traits\MultiDatabase; class UserRepository extends BaseRepository implements \Laravelplus\RepositoryPattern\Contracts\MultiDatabaseInterface { use SoftDeletes, Cacheable, MultiDatabase; protected static string $modelClass = User::class; // Optionally configure $table, $primaryKey, $connection, etc. }
Using Trait Methods
$userRepo = new UserRepository(); $userRepo->softDelete($userId); // Soft delete a user $userRepo->restore($userId); // Restore a soft-deleted user $userRepo->cacheAll(30); // Cache all users for 30 minutes $userRepo->runOnConnection('mysql2', fn($db) => $db->table('users')->get());
Available Traits
SoftDeletes: Adds soft delete, restore, and onlyTrashed methods.Cacheable: Adds cacheAll for caching results.Loggable: Adds logAction for logging repository actions.Eventable: Adds fireEvent for dispatching events.ValidatesData: Adds validate for validating data before create/update.Searchable: Adds search for column-based LIKE search.Sortable: Adds sortBy for sorting results.HasRelationships: Adds withRelations for eager loading relationships.MultiDatabase: Adds runOnConnection and crossConnectionQuery for multi-database support.
Mix and match these traits in your repositories as needed!
Repository Generator Command
You can quickly generate repository classes using the built-in Artisan command:
Simple Repository
Generate a basic repository (no traits or interface):
php artisan make:repository User
This uses the stubs/repository.simple.stub template.
Repository with Traits and Interface
Generate a repository with traits and/or an interface:
php artisan make:repository User --traits=SoftDeletes,Cacheable --interface=MultiDatabaseInterface
This uses the stubs/repository.stub template and will insert the specified traits and interface.
Customizing Stubs
You can publish the stubs to your application and customize them as needed:
php artisan vendor:publish --tag=config
This will copy the stubs to your stubs/ directory, where you can edit them to fit your project's needs.
laravelplus/repository-pattern 适用场景与选型建议
laravelplus/repository-pattern 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「repository-pattern」 「laravelPlus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laravelplus/repository-pattern 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laravelplus/repository-pattern 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laravelplus/repository-pattern 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Decorate your repositories and make them cacheable
Repository Design Pattern implementation for Laravel
Flexible and powerful repositories for laravel framework
All the basic methods to any Laravel project with Repository pattern
Fortress is a powerful Laravel package designed to streamline and enhance attribute-based authorization through middleware. It acts as the ultimate security gatekeeper for your application, ensuring that only the right users with the correct attributes gain access to specific resources.
DTO-first API runtime foundation for CodeIgniter 4: base classes, HTTP layer, services, repositories, filters, audit chain, and queue. Powers ci4-api-starter and ci4-domain-starter. Pair with dcardenasl/ci4-api-scaffolding for CRUD generation.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-18