motrotz/relation-attributes
Composer 安装命令:
composer require motrotz/relation-attributes
包简介
Define Eloquent relationships using Attributes.
README 文档
README
Define Eloquent relationships using PHP attributes for cleaner, more declarative models.
Features and Benefits
- Declarative Syntax: Define relationships using PHP 8 attributes directly on your model class
- Supports Core Relationships: BelongsTo, HasOne, HasMany, and BelongsToMany (more coming soon)
- Cleaner Models: Reduce boilerplate by eliminating repetitive relationship methods
- Better Organization: See all relationships at a glance at the top of your model
- Zero Configuration: Works with Laravel's existing relationship system
- Type Safety: IDEs can better understand and autocomplete your relationships
- Flexible Naming: Auto-generates relation names or specify custom ones
Installation
composer require motrotz/relation-attributes
Usage
Example Setup
Add the HasRelationAttributes trait to your Eloquent model and define relationships using attributes. You can be as detailed as needed, or as simple as you want. You can even have multiple relationships of the same type:
use Illuminate\Database\Eloquent\Model; use RelationAttributes\Concerns\HasRelationAttributes; use RelationAttributes\Attributes\{BelongsTo, HasOne, HasMany}; #[BelongsTo(Team::class)] #[BelongsTo(Team::class, 'personalTeam', 'personal_team_id')] #[HasOne(Profile::class)] #[HasMany(Post::class, 'articles', 'author_uuid', 'uuid')] #[HasMany(Notification::class)] #[HasMany( Post::class, 'publishedPosts', where: ['published' => true], orderBy: ['created_at' => 'desc'], limit: 5 )] class User extends Model { use HasRelationAttributes; } // Now you can access relationships as usual: $user->team; // BelongsTo relationship $user->personalTeam; $user->profile; // HasOne relationship $user->articles; $user->notifications; // HasMany relationship (auto-pluralized) $user->publishedPosts; // Filtered & sorted HasMany
Supported Relationships
| Relationship | Status | Example |
|---|---|---|
| BelongsTo | ✅ Supported | #[BelongsTo(Team::class)] |
| HasOne | ✅ Supported | #[HasOne(Profile::class)] |
| HasMany | ✅ Supported | #[HasMany(Post::class)] |
| BelongsToMany | ✅ Supported | #[BelongsToMany(Role::class)] |
| MorphTo | 🚧 Coming Later | Polymorphic relationships |
| MorphOne | 🚧 Coming Later | Polymorphic relationships |
| MorphMany | 🚧 Coming Later | Polymorphic relationships |
How It Works
The package uses PHP reflection to read attributes defined on your model class during initialization. It then automatically registers these as Eloquent relationships using Laravel's resolveRelationUsing() method. This means:
- All Eloquent features work as expected (eager loading, querying, etc.)
- No performance overhead after initialization
- Fully compatible with existing Laravel applications
- Relationships are resolved lazily, just like regular Eloquent
Testing
composer test
Documentation
The full documentation can be found on the project wiki.
External References
- Laravel Pluralization Documentation - Learn more about Laravel's pluralization rules used by this package
License
MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Credits
motrotz/relation-attributes 适用场景与选型建议
motrotz/relation-attributes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 motrotz/relation-attributes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 motrotz/relation-attributes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-16