thybag/bonus-laravel-relations
Composer 安装命令:
composer require thybag/bonus-laravel-relations
包简介
Additional eloquent ORM relation types for laravel.
README 文档
README
A selection of weird & wonderful additional relation types for laravel's eloquent ORM I've ended up needing.
Many of these are experimental and may behave in unexpected & none standard ways.
On the bright side - it's tested!
All licensed under the MIT license.
Usage.
- Install via composer using
composer require thybag/bonus-laravel-relations - Include the
use thybag\BonusLaravelRelations\Traits\BonusRelationsTrait;in to your model (or base model if you want them everywhere.) - Use the relations as you would any other.
The relation traits can also be added individually if you prefer that.
- Run tests with
composer test - Run lint with
composer lint
Relations
BelongsToMorph
Get polymorphic relations of a single type.
public function shop() { return $this->belongsToMorph(Shop::class, 'noteable'); }
HasManyViaMany
Define a relation via an unlimited number of middle tables.
public function products() { return $this->hasManyViaMany(Product::class)->via(Shop::class)->via(Franchise::class); }
HasAggregate
Get an aggregate result as a relation. The main benefit of this approach is it allows easy lazy loading of this data on collections + interaction with the results in a more eloquent like way.
public function productTotals() { return $this->hasAggregate(Product::class)->selectRaw(' COUNT(DISTINCT products.id) AS unique_products, SUM(products.amount) * AVG(products.value) AS stock_value, SUM(products.amount) AS total_products, AVG(products.value) AS average_product_value '); }
Additional transformations on data returned from hasAggregate can be defined by using withCallback(function($result){}) to define a transform function. Only one can be defined per relationship at present.
HasMethod
Use a local method as if it were a relation.
public function totalValue() { return $this->hasMethod(function () { return ['total' => ($this->amount * $this->value)]; }); }
BelongsToOne
Define a one-to-one relation through a pivot table.
public function latestRating() { return $this->belongsToOne(Rating::class, 'shop_rating')->latest('created_at'); }
InertModel
As the relationships HasMethod and HasAggregate don't return a traditional model from the database, a special model called type InertModel is used. This allows the relationship to fill the model with arbitrary attributes without the risk of causing unexpected behavior.
If you would like to use a custom InertModel rather than the one provided, create a config called bonus-laravel-relationships.php in your config folder, then set the value inertModel with the class path to the model you would like to use instead. I would recommend whatever model you use inherits from the base IntertModel to avoid unexpected behavior. Alternatively the hasMethod relationship supports a withModel or passing a model parameter to override this. Similar functionality is available in hasAggrgegate using the returnModel parameter.
thybag/bonus-laravel-relations 适用场景与选型建议
thybag/bonus-laravel-relations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35.27k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2018 年 06 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 thybag/bonus-laravel-relations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thybag/bonus-laravel-relations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 35.27k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-30