PHPackages                             joecianflone/modelproperties - 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. joecianflone/modelproperties

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

joecianflone/modelproperties
============================

Make the DX of Eloquent Models better

3.1.1(9mo ago)11.1k[5 PRs](https://github.com/JoeCianflone/ModelProperties/pulls)MITPHPPHP ^8.4CI passing

Since Feb 17Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/JoeCianflone/ModelProperties)[ Packagist](https://packagist.org/packages/joecianflone/modelproperties)[ Docs](https://github.com/joecianflone/modelproperties)[ RSS](/packages/joecianflone-modelproperties/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (12)Versions (13)Used By (0)

Model Properties
================

[](#model-properties)

As a developer, you may have experienced the cognitive load of dealing with arrays to define various model behaviors in Laravel, such as fillable, guarded, casting, default values, and hidden properties. I found myself wondering if there was a more streamlined way to handle this. What if we could use PHP attributes on the class itself to define these properties? This idea led me to explore this approach, which I share in [this video](https://www.youtube.com/watch?v=n8-SHq4zSr4)

Support Me
----------

[](#support-me)

Buy me a coffee or something, but more importantly would be if you used this and like it, if you'd talk about this and also tell me if you have any issues with it.

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

[](#installation)

```
composer require joecianflone/modelproperties
```

Usage
-----

[](#usage)

Here's a standard `User` model where you've got some fillable fields and a few casts and whatnot:

```
class User extends Model {

    protected $fillable = ['name', 'email', 'password', 'role', 'is_active'];

    protected $hidden = ['password', 'remember_token'];

    protected $casts = [
        'id' => 'string',
        'role' => UserRole::class
        'is_active' => 'boolean',
        'email_activated_at' => 'datetime',
        'remember_token' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    protected $attributes = [
        'role' => UserRole::STANDARD
        'is_active' => true,
    ];
}
```

Again, my gripe here is that it takes a bit of work to understand all the fields this model has. It's pretty easy to miss that the `id` is a string if you were looking quickly...and oh crap, I forgot to turn off auto-incrementing did you catch that? How about the fact that the password should be cast and I forgot that one too

So using the model properties it would now look like this:

```
#[ModelProperties(
    id:  ['string', 'is:primary' 'auto-increment' => false],
    name:  ['string', 'is:fillable'],
    email:  ['string', 'is:fillable'],
    password:  ['hashed', 'is:hidden|fillable'],
    role: [UserRole::class => UserRole::STANDARD],
    is_active:          ['boolean' => true],
    remember_token:     ['string', 'is:hidden'],
    email_verified_at:  ['datetime'],
    created_at:         ['datetime'],
    updated_at:         ['datetime'],
)]
class User extends Model {

    use HasModelProperties;

}
```

That's it. Add the `HasModelProperties` trait and you're good to start using the `ModelProperties` attribute and have a better DX.

Config Settings
---------------

[](#config-settings)

Model properties has a few configurations you can change and they're publishable.

```
$ php artisan vendor:publish --provider="JoeCianflone\ModelProperties\ModelPropertiesServiceProvider"
```

### Mass Assignment

[](#mass-assignment)

Turn this off at your own risk! By default, this will set `$guarded = ['*']` you should leave it this way.

```
    [
         'mass_assignment_protection' => true, // default
    ]
```

### Default Mass Assignment Mode

[](#default-mass-assignment-mode)

Now if you don't want to set all your properties to either guareded or fillable on all your models you can change this to be either 'guarded' or 'fillable' and then you don't have to worry about being explicit.

NOTE: with `mass_assignment_protection` turned on, you really never have to expliclty set something to `guarded` they'll really be discarded because `$guarded = ['*']`. If you DO turn off `mass_assignment_protection` you're going to NEED to set this to guarded or else we'll throw an exception with this set to either 'none' or 'fillable'.

*Honestly, think twice before doing that. Your best bet here is to leave mass\_assignment\_protection set to true and, if anything, set this to fillable. That's basically Laravel's default behavior and setting all the props over to the fillable array.*

```
    [
        // values could be 'none' | 'fillable' | 'guarded'
        'default_property_assignment' => 'none', // default
    ]
```

### Explicitly Cast Strings

[](#explicitly-cast-strings)

```
    [
        'explicity_cast_strings' => false // default
    ]
```

In the above examples, see how how told the model that some properties were strings? You don't actually need to cast strings as strings in laravel, but some people like doing that, so if you'd like to be explicit, set this to true.

How it works
------------

[](#how-it-works)

Under the hood, we're not doing too many fancy things. At the end of the day, the attribute will get transformed into a PHP object and we "just use" all the standard Laravel model variables to populate fillable, guarded and whatever else. The only magic here, if there is any, is Laravels own built-in ability to automatically execute a method in a trait.

So what is this doing? Mostly parsing the attributes and dumping them in the right places. It adds no extra cruft to the model and if you were to `dd()` a model that uses `ModelProperties` and a model that does not, you'd see no difference in the object created.

Testing
-------

[](#testing)

This plugin uses Pest for our Unit and Architecture tests, if you'd like to run them yourself you can use...

```
composer test
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Joe Cianflone](https://github.com/JoeCianflone)

License
-------

[](#license)

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

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance74

Regular maintenance activity

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity66

Established project with proven stability

 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 ~107 days

Recently: every ~134 days

Total

6

Last Release

282d ago

Major Versions

0.1.1 → 1.0.02024-02-18

1.0.0 → 2.0.02025-02-26

2.0.0 → 3.0.02025-07-28

PHP version history (3 changes)0.1.1PHP ^8.1

2.0.0PHP ^8.2

3.0.0PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/a64058c76d1817832ce887219a90c6dfcdd292493d8ece80e5b977bc9dc4bc64?d=identicon)[Joe Cianflone](/maintainers/Joe%20Cianflone)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (23 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (21 commits)")[![JoeCianflone](https://avatars.githubusercontent.com/u/989648?v=4)](https://github.com/JoeCianflone "JoeCianflone (21 commits)")

---

Tags

laraveleloquentattributesmodelsmodel-attributesdeveloper-experiencemodel-properties

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/joecianflone-modelproperties/health.svg)

```
[![Health](https://phpackages.com/badges/joecianflone-modelproperties/health.svg)](https://phpackages.com/packages/joecianflone-modelproperties)
```

###  Alternatives

[silber/bouncer

Eloquent roles and abilities.

3.6k4.4M25](/packages/silber-bouncer)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[lacodix/laravel-model-filter

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

17649.9k](/packages/lacodix-laravel-model-filter)

PHPackages © 2026

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