nathandunn/model-repositories
Composer 安装命令:
composer require nathandunn/model-repositories
包简介
Easily create a repository-like entities using a Laravel Model
README 文档
README
A lightweight abstraction for Laravel Eloquent models that enables cleaner, more expressive repository-style access to your application's data layer.
"Fetching collections or business-specific queries from models violates separation of concerns. This package provides an elegant way to wrap Eloquent models into repository-like entities."
Inspired by Jack Wagstaffe
🚀 Installation
Install via Composer:
composer require nathandunn/model-repositories
📦 What This Provides
This package allows you to:
- Encapsulate complex query logic outside your Eloquent models.
- Maintain proper separation between models and querying logic.
- Leverage dynamic method chaining like
getPaginatedForUser()orgetForXyz().
🧠 Concept
Instead of injecting Eloquent models directly into services or controllers, create a repository class per model that encapsulates query logic.
For example, instead of:
$records = Record::where('user_id', $user->id)->paginate();
You can use:
$records = $recordRepository->getPaginatedForUser($user);
🛠 Usage
1. Create a Custom Repository
<?php namespace App\Records; use App\Users\User; use Illuminate\Database\Eloquent\Builder; use NathanDunn\Repositories\Repository; class RecordRepository extends Repository { public function __construct(Record $record) { parent::__construct($record); } public function forUser(User $user): Builder { return $this->model->where('user_id', '=', $user->id); } }
2. Inject into Your Controller
<?php namespace App\Http\Controllers; use App\Http\Resources\RecordResource; use App\Records\RecordRepository; use Illuminate\Http\Request; class RecordsController extends Controller { public function __construct( protected RecordRepository $recordRepository ) {} public function index(Request $request) { $records = $this->recordRepository->getPaginatedForUser($request->user()); return RecordResource::collection($records); } }
🔁 Dynamic Method Chaining
The base Repository class supports auto-chaining based on naming convention. The following helper prefixes are recognised:
| Prefix | Description |
|---|---|
get |
Fetch a collection |
getPaginated |
Fetch and paginate |
getCount |
Get count of query results |
first / firstOrFail |
Fetch a single item |
exists |
Check for existence |
So calling getPaginatedForUser($user) will:
- Call
forUser($user)(your custom method) - Then call
paginate()on the result
This is handled by __call() in the base class using a naming convention-based resolver.
🧪 Example Call Chain
Given a repository method forUser(User $user): Builder, these dynamic calls become possible:
$repo->getForUser($user); // ->forUser()->get() $repo->getPaginatedForUser($user); // ->forUser()->paginate() $repo->getCountForUser($user); // ->forUser()->count()
✅ Requirements
- Laravel 10+
- PHP 8.1+
📚 License
MIT © Nathan Dunn
nathandunn/model-repositories 适用场景与选型建议
nathandunn/model-repositories 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.55k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nathandunn/model-repositories 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nathandunn/model-repositories 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 17.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2023-02-18