lerouse/laravel-repository
Composer 安装命令:
composer require lerouse/laravel-repository
包简介
Repository implementation for Laravel.
README 文档
README
Model Repository implementation for the Laravel framework ^v6.0 and above.
This package provides a structured framework to begin implementing a simple Model Repository structure in an existing Laravel application.
Installation
The recommended method to install LaravelRepository is with composer
php composer require lerouse/laravel-repository
Laravel without auto-discovery
If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php
\Lerouse\LaravelRepository\LaravelRepositoryServiceProvider::class,
Package configuration
Copy the package configuration to your local config directory.
php artisan vendor:publish --tag=repository-config
Usage
Model Repository Location
By default Laravel Model Repository presumes that model repositories are stored in the directory
app/repositories/model. If you would like to change the directory your model repositories are stored change the namespace
variable in the config/repostiory.php config file.
'namespace' => '\App\Repositories\Model',
For example if your model repositories are located in app/model/repositories update the namespace to
'namespace' => '\App\Model\Repositories',
Creating a Repository
Repositories should extend the Lerouse\LaravelRepository\EloquentRepository method and implement the builder method
which should return an instance of a Illuminate\Database\Eloquent\Builder.
<?php namespace App\Repositories\Models; use Illuminate\Database\Eloquent\Builder; use Lerouse\LaravelRepository\EloquentRepository; use App\Models\User; class UserRepository extends EloquentRepository { /** * Get the Repository Query Builder * * @return Builder */ public function builder(): Builder { return User::query(); } }
The following methods are available out of the box:-
| Method | Return Type | Description |
|---|---|---|
all |
Collection | Get all Models |
paginate |
LengthAwarePaginator | Get all models as Paginated Results |
find |
Model | Find a Model by its Primary Key |
findMany |
Collection | Find a collection of Models by their Primary Keys |
create |
Model | Create a new Model |
createMany |
Boolean | Create a collection of new Models |
update |
Model | Update a Model by its Primary Key |
updateMany |
Boolean | Update a collection of Models |
delete |
Model/null | Delete a Model by its Primary Key |
deleteMany |
void | Delete a collection of Models by their Primary Keys |
Using a Repository
Repositories can be used directly (see above for available built in methods).
<?php namespace App\Http\Controllers; use Illuminate\Database\Eloquent\Builder; class UserController { /** * Get the Repository Query Builder * * @return Builder */ public function index(): Builder { $repository = new UserRepository; return $repository->all(); } }
Extending a Repository
Repositories should be the single point for extending/amending queries to your models. The advantage of this is all of your model business logic is in a single location. See below a repository that contains commonly used extended functionality as well as some custom methods that are also used.
<?php namespace App\Repositories\Models; use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Database\Eloquent\Builder; use Lerouse\LaravelRepository\EloquentRepository; use Illuminate\Database\Eloquent\Collection; use App\Models\Team; use App\Models\User; class UserRepository extends EloquentRepository { /** * Get all models as Paginated Results * * @param int|null $limit * @param int $page * @return LengthAwarePaginator */ public function paginate(int $limit = null, int $page = 1): LengthAwarePaginator { return $this->builder() ->orderBy($this->getModelTable() . '.created_at', 'desc') ->paginate($limit ?? 25, $page); } /** * Get active Users * * @return Collection */ public function active(): Collection { return $this->builder() ->where($this->getModelTable() . 'active', '=', true) ->orderBy($this->getModelTable() . '.created_at', 'desc') ->get(); } /** * Get the Repository Query Builder * * @return Builder */ public function builder(): Builder { return User::query() ->select($this->getModelTable() . '.*') ->selectRaw('teams.name as user_team_name') ->leftJoin('teams', 'teams.id', '=', $this->getModelTable() . '.team_id'); } }
Laravel Route Model Binding
Laravel route model binding provides a convenient way to automatically inject the model instances directly into your
routes. To force the route model binding to use your repository rather than the model itself you can add the
Lerouse\LaravelRepository\HasRepository trait to your model.
<?php namespace App\Http\Controllers; use Illuminate\Database\Eloquent\Model; use Lerouse\LaravelRepository\HasRepository; class User extends Model { use HasRepository; }
Now when you utilise Laravel's Route Model Binding, the find method on the repository will be used.
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Database\Eloquent\Builder; class UserController extends Controller { /** * Get the Repository Query Builder * * @return Builder */ public function show(User $user): Builder // binding resolved through the UserRepository's find method { return $user; } }
Please Note: The repositories location is determined via the namespace variable in the config/repostiory.php
config file.
'namespace' => '\App\Repositories\Model',
License
Laravel Model Repository is free software distributed under the terms of the MIT license.
lerouse/laravel-repository 适用场景与选型建议
lerouse/laravel-repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.11k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2020 年 10 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「repository」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lerouse/laravel-repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lerouse/laravel-repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lerouse/laravel-repository 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
repository php library
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 17.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 10
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-06