This package is abandoned and no longer maintained. No replacement package was suggested.

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

v0.2.0 2023-02-01 12:18 UTC

This package is auto-updated.

Last update: 2023-03-29 12:36:11 UTC


run-tests PHPStan

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


You can install the package via composer:

composer require worksome/model-attributes


Assuming we have the following table structure:

- id

- id
- user_id
- stars

And the following models:

class User extends Model 
    public function reviews()
class Review extends Model 
    public function user()

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();

                $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(

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:


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



composer test


Please see CHANGELOG for more information on what has changed recently.



The MIT License (MIT). Please see License File for more information.