PHPackages                             motrotz/relation-attributes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. motrotz/relation-attributes

ActiveLibrary[Database &amp; ORM](/categories/database)

motrotz/relation-attributes
===========================

Define Eloquent relationships using Attributes.

00PHP

Since Sep 16Pushed 9mo agoCompare

[ Source](https://github.com/Motrotz/relation-attributes)[ Packagist](https://packagist.org/packages/motrotz/relation-attributes)[ RSS](/packages/motrotz-relation-attributes/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Relation Attributes for Laravel
===============================

[](#relation-attributes-for-laravel)

Define Eloquent relationships using PHP attributes for cleaner, more declarative models.

Features and Benefits
---------------------

[](#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
------------

[](#installation)

```
composer require motrotz/relation-attributes
```

Usage
-----

[](#usage)

### Example Setup

[](#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
-----------------------

[](#supported-relationships)

RelationshipStatusExampleBelongsTo✅ Supported`#[BelongsTo(Team::class)]`HasOne✅ Supported`#[HasOne(Profile::class)]`HasMany✅ Supported`#[HasMany(Post::class)]`BelongsToMany✅ Supported`#[BelongsToMany(Role::class)]`MorphTo🚧 Coming LaterPolymorphic relationshipsMorphOne🚧 Coming LaterPolymorphic relationshipsMorphMany🚧 Coming LaterPolymorphic relationshipsHow It Works
------------

[](#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
-------

[](#testing)

```
composer test
```

Documentation
-------------

[](#documentation)

The full documentation can be found on the [project wiki](https://github.com/Motrotz/relation-attributes/wiki).

External References
-------------------

[](#external-references)

- [Laravel Pluralization Documentation](https://laravel.com/docs/12.x/localization#pluralization-language) - Learn more about Laravel's pluralization rules used by this package

License
-------

[](#license)

MIT License - see the [LICENSE](LICENSE) file for details.

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

Credits
-------

[](#credits)

- Originally created by [killmails](https://github.com/killmails)
- Maintained by [Motrotz](https://github.com/Motrotz)

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance40

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 Bus Factor1

Top contributor holds 78.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/130649391?v=4)[Motrotz](/maintainers/Motrotz)[@Motrotz](https://github.com/Motrotz)

---

Top Contributors

[![syzio](https://avatars.githubusercontent.com/u/26165164?v=4)](https://github.com/syzio "syzio (11 commits)")[![Motrotz](https://avatars.githubusercontent.com/u/130649391?v=4)](https://github.com/Motrotz "Motrotz (3 commits)")

### Embed Badge

![Health badge](/badges/motrotz-relation-attributes/health.svg)

```
[![Health](https://phpackages.com/badges/motrotz-relation-attributes/health.svg)](https://phpackages.com/packages/motrotz-relation-attributes)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[pgvector/pgvector

pgvector support for PHP

198741.5k12](/packages/pgvector-pgvector)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
