PHPackages                             hnhdigital-os/laravel-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. hnhdigital-os/laravel-model-attributes

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

hnhdigital-os/laravel-model-attributes
======================================

Changes how the Eloquent Model provides attributes.

3.0.2(2y ago)127MITPHPPHP ^8.0CI failing

Since Feb 3Pushed 2y agoCompare

[ Source](https://github.com/hnhdigital-os/laravel-model-attributes)[ Packagist](https://packagist.org/packages/hnhdigital-os/laravel-model-attributes)[ RSS](/packages/hnhdigital-os-laravel-model-attributes/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (5)Versions (43)Used By (0)

```
___  ___          _      _ _____      _
|  \/  |         | |    | /  ___|    | |
| .  . | ___   __| | ___| \ `--.  ___| |__   ___ _ __ ___   __ _
| |\/| |/ _ \ / _` |/ _ \ |`--. \/ __| '_ \ / _ \ '_ ` _ \ / _` |
| |  | | (_) | (_| |  __/ /\__/ / (__| | | |  __/ | | | | | (_| |
\_|  |_/\___/ \__,_|\___|_\____/ \___|_| |_|\___|_| |_| |_|\__,_|

```

Combines the casts, fillable, and hidden properties into a single schema array property amongst other features.

Other features include:

- Custom configuration entries against your model attributes.
- Store your attribute rules against your model and store and validate your model data automatically.
- Custom casting is now possible using this package! See [Custom casts](#custom-casts).

[![Latest Stable Version](https://camo.githubusercontent.com/275086d616b6a4cec23aa7558f8bc607f97e13991e4d461ce83355708dc99155/68747470733a2f2f706f7365722e707567782e6f72672f686e686469676974616c2d6f732f6c61726176656c2d6d6f64656c2d736368656d612f762f737461626c652e737667)](https://packagist.org/packages/hnhdigital-os/laravel-model-schema) [![Total Downloads](https://camo.githubusercontent.com/8a91fe60be1907749dcd9de7f48261d5dbe0cc96f04e0c6374db088c63875a51/68747470733a2f2f706f7365722e707567782e6f72672f686e686469676974616c2d6f732f6c61726176656c2d6d6f64656c2d736368656d612f646f776e6c6f6164732e737667)](https://packagist.org/packages/hnhdigital-os/laravel-model-schema) [![Latest Unstable Version](https://camo.githubusercontent.com/9de9348e069fca2247f956652908a6cf595789f3bfa09ccad6931468a9725eb9/68747470733a2f2f706f7365722e707567782e6f72672f686e686469676974616c2d6f732f6c61726176656c2d6d6f64656c2d736368656d612f762f756e737461626c652e737667)](https://packagist.org/packages/hnhdigital-os/laravel-model-schema) [![Built for Laravel](https://camo.githubusercontent.com/ddd4a07bb769306a9338cffde2032c289e4d43d43ac346122709465a47cdff3f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4275696c745f666f722d4c61726176656c2d677265656e2e737667)](https://laravel.com/) [![License](https://camo.githubusercontent.com/cddfd1ad4c74efb7373f6ee8c788cd5bd1fd4c496e395447af824227b09dcf2c/68747470733a2f2f706f7365722e707567782e6f72672f686e686469676974616c2d6f732f6c61726176656c2d6d6f64656c2d736368656d612f6c6963656e73652e737667)](https://packagist.org/packages/hnhdigital-os/laravel-model-schema) [![Donate to this project using Patreon](https://camo.githubusercontent.com/f9e075baad95563481d35174d43ef50757281abb6bc795d0f473fad452afa030/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f70617472656f6e2d646f6e6174652d79656c6c6f772e737667)](https://patreon.com/RoccoHoward)

This package has been developed by H&amp;H|Digital, an Australian botique developer. Visit us at [hnh.digital](http://hnh.digital).

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

[](#documentation)

- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Configuration](#configuration)
- [Custom casts](#custom-casts)
- [Contributing](#contributing)
- [Reporting issues](#reporting-issues)
- [Credits](#credits)
- [License](#license)

Prerequisites
-------------

[](#prerequisites)

- PHP &gt;= 8.0.2
- Laravel &gt;= 9.0

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

[](#installation)

Via composer:

`$ composer require hnhdigital-os/laravel-model-schema ~3.0`

Configuration
-------------

[](#configuration)

### Enable the model

[](#enable-the-model)

Enable the model on any given model.

```
use HnhDigital\ModelSchema\Model;

class SomeModel extends Model
{

}
```

We recommend implementing a shared base model that you extend.

### Convert your current properties

[](#convert-your-current-properties)

The schema for a model is implemented using a protected property.

Here's an example:

```
    /**
     * Describe your model.
     *
     * @var array
     */
    protected static $schema = [
        'id' => [
            'cast'    => 'integer',
            'guarded' => true,
        ],
        'name' => [
            'cast'     => 'string',
            'rules'    => 'max:255',
            'fillable' => true,
        ],
        'created_at' => [
            'cast'    => 'datetime',
            'guarded' => true,
            'log'     => false,
            'hidden'  => true,
        ],
        'updated_at' => [
            'cast'    => 'datetime',
            'guarded' => true,
            'hidden'  => true,
        ],
        'deleted_at' => [
            'cast'    => 'datetime',
            'rules'  => 'nullable',
            'hidden'  => true,
        ],
    ];
```

Ensure the parent boot occurs after your triggers so that any attribute changes are done before this packages triggers the validation.

```
    /**
     * Boot triggers.
     *
     * @return void
     */
    public static function boot()
    {
        self::updating(function ($model) {
            // Doing something.
        });

        parent::boot();
    }
```

Models implementing this package will now throw a ValidationException exception if they do not pass validation. Be sure to catch these.

```
    try {
        $user = User::create(request()->all());
    } catch (HnhDigital\ModelSchema\Exceptions\ValidationException $exception) {
        // Do something about the validation.

        // You can add things to the validator.
        $exception->getValidator()->errors()->add('field', 'Something is wrong with this field!');

        // We've implemented a response.
        // This redirects the same as a validator with errors.
        return $exception->getResponse('user::add');
    }
```

Custom casts
------------

[](#custom-casts)

This package allows the ability to add custom casts. Simply create a trait, and register the cast on boot.

```
trait ModelCastAsMoneyTrait
{
    /**
     * Cast value as Money.
     *
     * @param mixed $value
     *
     * @return Money
     */
    protected function castAsMoney($value, $currency = 'USD', $locale = 'en_US'): Money
    {
        return new Money($value, $currency, $locale);
    }

    /**
     * Convert the Money value back to a storable type.
     *
     * @return int
     */
    protected function castMoneyToInt($key, $value): int
    {
        if (is_object($value)) {
            return (int) $value->amount();
        }

        return (int) $value->amount();
    }

    /**
     * Register the casting definitions.
     */
    public static function bootModelCastAsMoneyTrait()
    {
        static::registerCastFromDatabase('money', 'castAsMoney');
        static::registerCastToDatabase('money', 'castMoneyToInt');
        static::registerCastValidator('money', 'int');
    }
}
```

Defining your attributes would look like this:

```
    ...

    'currency' => [
        'cast'     => 'string',
        'rules'    => 'min:3|max:3',
        'fillable' => true,
    ],
    'total_amount' => [
        'cast'        => 'money',
        'cast-params' => '$currency:en_US',
        'default'     => 0,
        'fillable'    => true,
    ],
    ...
```

Casting parameters would include a helper function, or a local model method.

eg `$currency:user_locale()`, or `$currency():$locale()`

### Available custom casts

[](#available-custom-casts)

- Cast the attribute to [Money](https://github.com/hnhdigital-os/laravel-model-schema-money)

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

[](#contributing)

Please review the [Contribution Guidelines](https://github.com/hnhdigital-os/laravel-model-schema/blob/master/CONTRIBUTING.md).

Only PRs that meet all criterium will be accepted.

Reporting issues
----------------

[](#reporting-issues)

When reporting issues, please fill out the included [template](https://github.com/hnhdigital-os/laravel-model-schema/blob/master/ISSUE_TEMPLATE.md) as completely as possible. Incomplete issues may be ignored or closed if there is not enough information included to be actionable.

Code of conduct
---------------

[](#code-of-conduct)

Please observe and respect all aspects of the included [Code of Conduct](https://github.com/hnhdigital-os/laravel-model-schema/blob/master/CODE_OF_CONDUCT.md).

Credits
-------

[](#credits)

- [Rocco Howard](https://github.com/RoccoHoward)
- [All Contributors](https://github.com/hnhdigital-os/laravel-model-schema/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/hnhdigital-os/laravel-model-schema/blob/master/LICENSE) for more information.

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 97.9% 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.

###  Release Activity

Cadence

Every ~49 days

Recently: every ~32 days

Total

42

Last Release

1028d ago

Major Versions

v1.x-dev → 2.0.02021-08-16

v2.x-dev → 3.0.02023-05-11

PHP version history (6 changes)1.0.0PHP &gt;=7.1.0

1.4.0PHP &gt;=7.2.0

1.4.1PHP ^7.2

2.0.0PHP ^7.3

3.0.0PHP ^8.0.2

3.0.1PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5270e3a3d1add4e39f12dfde3ce549999a26f759d7128056083ab07f475259f2?d=identicon)[bluora](/maintainers/bluora)

---

Top Contributors

[![RoccoHoward](https://avatars.githubusercontent.com/u/227896?v=4)](https://github.com/RoccoHoward "RoccoHoward (139 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (3 commits)")

---

Tags

laravelattributesilluminate

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hnhdigital-os-laravel-model-attributes/health.svg)

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

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11223.5M33](/packages/anourvalar-eloquent-serialize)[tpetry/laravel-postgresql-enhanced

Support for many missing PostgreSQL specific features

1.0k2.4M28](/packages/tpetry-laravel-postgresql-enhanced)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[ramadan/easy-model

A Laravel package for enjoyably managing database queries.

111.6k](/packages/ramadan-easy-model)

PHPackages © 2026

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