PHPackages                             worksome/model-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. worksome/model-attributes

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

worksome/model-attributes
=========================

Model attributes are dynamically generated data on models added as a relationship.

v0.2.0(3y ago)4250.5k2MITPHPPHP ^8.2

Since Feb 24Pushed 3y ago3 watchersCompare

[ Source](https://github.com/worksome/model-attributes)[ Packagist](https://packagist.org/packages/worksome/model-attributes)[ Docs](https://github.com/worksome/model-attributes)[ RSS](/packages/worksome-model-attributes/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (9)Versions (3)Used By (0)

[![run-tests](https://github.com/worksome/model-attributes/actions/workflows/run-tests.yml/badge.svg)](https://github.com/worksome/model-attributes/actions/workflows/run-tests.yml)[![PHPStan](https://github.com/worksome/model-attributes/actions/workflows/phpstan.yml/badge.svg)](https://github.com/worksome/model-attributes/actions/workflows/phpstan.yml)

Model attributes are dynamically generated values for models. They are used as eloquent relationships which can be eager loaded.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require worksome/model-attributes
```

Usage
-----

[](#usage)

Assuming we have the following table structure:

```
users:
- id

reviews:
- id
- user_id
- stars

```

And the following models:

```
class User extends Model
{
    public function reviews()
    {
        $this->hasMany(Review::class);
    }
}

```

```
class Review extends Model
{
    public function user()
    {
        $this->belongsTo(User::class);
    }
}

```

We can add a `rating` property to each user that is the calculated average of a users reviews. First we're going to create a Model attribute:

```
class Rating extends \Worksome\ModelAttributes\ModelAttribute
{
    protected $casts = [
        'id' => 'int',
        'user_id' => 'int',
        'rating' => 'float',
    ];

    public static function attributeGlobalScope(Builder $query): void
    {
        $ratingModel = new Rating();

        $query
            ->groupBy($ratingModel->user()->getForeignKeyName())
            ->addSelect([
                $ratingModel->getKeyName(), // id
                $ratingModel->user()->getForeignKeyName(), // user_id
                \DB::raw('avg(stars) as rating'), // rating
            ]);
    }
}

```

and add it as a relationship to the user model

```
public function rating(): \Worksome\ModelAttributes\AttributeRelation|\Illuminate\Database\Eloquent\Relations\HasOne
{
    return new \Worksome\ModelAttributes\AttributeRelation(
        $this->hasOne(Rating::class)
    );
}

```

So that it can be used like so:

```
$user = User::first();
$rating = $user->rating; // The rating model attribute created above
$rating->rating // the actual rating

```

Since model attributes are essentially dynamically generated data, and the rating is a scalar, we can override the `getValue()` method of a model in order to "reach" the rating faster:

```
public function getValue()
{
    return $this->rating;
}

```

And now the rating can be accessed like so:

```
User::first()->rating;

```

And because it's a relationship it can be eager loaded:

```
User::with('rating')->get()->map->rating;

```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [Marian Hodorogea](https://github.com/ahmes)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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.

###  Release Activity

Cadence

Every ~342 days

Total

2

Last Release

1248d ago

PHP version history (2 changes)v0.1.0PHP ^8.0|^8.1

v0.2.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1984055?v=4)[Marian H](/maintainers/ahmes)[@ahmes](https://github.com/ahmes)

---

Top Contributors

[![lukeraymonddowning](https://avatars.githubusercontent.com/u/12202279?v=4)](https://github.com/lukeraymonddowning "lukeraymonddowning (16 commits)")[![ahmes](https://avatars.githubusercontent.com/u/1984055?v=4)](https://github.com/ahmes "ahmes (13 commits)")[![owenvoke](https://avatars.githubusercontent.com/u/1899334?v=4)](https://github.com/owenvoke "owenvoke (2 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

laraveleloquentworksomemodel-attributes

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/worksome-model-attributes/health.svg)

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

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.1k11.2M100](/packages/dedoc-scramble)[wnx/laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.

213420.1k2](/packages/wnx-laravel-backup-restore)[lacodix/laravel-model-filter

A Laravel package to filter, search and sort models with ease while fetching from database.

17558.6k](/packages/lacodix-laravel-model-filter)[spatie/laravel-passkeys

Use passkeys in your Laravel app

471890.7k39](/packages/spatie-laravel-passkeys)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)

PHPackages © 2026

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