stayallive/laravel-inverse-relations
Composer 安装命令:
composer require stayallive/laravel-inverse-relations
包简介
HasMany, HasOne and MorphMany Eloquent relations with inverse hydration.
关键字:
README 文档
README
HasMany, HasOne and MorphMany Eloquent relations with inverse hydration.
Jonathan Reinink wrote a great blog post about optimizing circular relationships in Laravel.
The package stancl/laravel-hasmanywithinverse ran with that idea and implemented it for the hasMany relation. This package improves on that by implementing it for the hasOne and morphMany relations too.
In short, this package allows you to define inverse relations on your models. This means that you can define a relation on a model that points to another model. This is useful when you have a circular relationship between two models and you want to be able to access the inverse relation without having to load the other model from the database potentially saving a lot of database queries.
Installation
composer require stayallive/laravel-inverse-relations
Usage
Adding the HasHasManyWithInverseRelation, HasHasOneWithInverseRelation and/or HasMorphManyWithInverseRelation traits will provide you with the helper methods to setup the inverse relations.
Check out the test stubs for examples on how to implement the traits on your models to setup the inverse relations:
Caveats
Because the inverse relation stores the parent by reference (not cloned), you need to be careful when you are using the relation since you will be modifying the parent model. This is nothing new to this package since eager loading does the same thing but I thought to mention it since it could catch you off guard.
There is one other downside to the inverse relation being stored as a reference since it creates a circular reference. This means that you cannot recusively serialize the model that has the inverse relation.
Laravel does this for example when you pass the model in a queued job since the SerializesModels trait will also try to store which relations were loaded when the model was queued to load those relations back causing once the job is being processed.
This causes an infinite loop trying to figure out which relations are loaded recursively, eating up memory until the process is killed by the OS or crashes.
To prevent this you can either pass your model to your job using $model->withoutRelations() or you can disable the relations loading on your model by adding the following method to your model:
public function getQueueableRelations(): array { return []; }
This disables the collection and loading of relations on the model when it (un-)serialized for the queued job.
Personally I put this in my base model since I never want to load the relations loaded when I push a job on the queue since they are mostly not useful in the context of the job and apart from guarding against circular references this also saves database queries (sometimes a lot) retrieving unused relations.
Security Vulnerabilities
If you discover a security vulnerability within this package, please send an e-mail to Alex Bouma at alex+security@bouma.me. All security vulnerabilities will be swiftly addressed.
License
This package is open-sourced software licensed under the MIT license.
stayallive/laravel-inverse-relations 适用场景与选型建议
stayallive/laravel-inverse-relations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.07k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2023 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「relations」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 stayallive/laravel-inverse-relations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stayallive/laravel-inverse-relations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 stayallive/laravel-inverse-relations 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Eloquent model updater for nested relations data for Laravel.
Missing relationship events for Laravel
Laravel package for inspecting model classes on relations en attributes.
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
统计信息
- 总下载量: 11.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-07