PHPackages                             kontoulis/laravel-attribute-model-casting - 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. kontoulis/laravel-attribute-model-casting

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

kontoulis/laravel-attribute-model-casting
=========================================

A Laravel package enabling the use of native PHP attributes for defining and managing Eloquent model attribute casts.

1.0.0(10mo ago)12MITPHP

Since Jun 20Pushed 10mo agoCompare

[ Source](https://github.com/kontoulis/laravel-attribute-model-casting)[ Packagist](https://packagist.org/packages/kontoulis/laravel-attribute-model-casting)[ RSS](/packages/kontoulis-laravel-attribute-model-casting/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

Laravel Attribute Model Casting
===============================

[](#laravel-attribute-model-casting)

[![Laravel 11+](https://camo.githubusercontent.com/122cad70dadb5c94d44586694b83a7fdf4b55edf4b343d98c8f4fdb999f39632/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31312532422d626c7565)](https://laravel.com)[![Packagist Downloads](https://camo.githubusercontent.com/05a507df1f0fddbce0eb6beac9190adec8d5726a39d7cc4127ae191ba679115a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b6f6e746f756c69732f6c61726176656c2d6174747269627574652d6d6f64656c2d63617374696e67)](https://packagist.org/packages/kontoulis/laravel-attribute-model-casting)[![License](https://camo.githubusercontent.com/43695ee230bf0eb9d2baaf80fd912c6fd12f83130b35ca88f032040e365b8405/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6b6f6e746f756c69732f6c61726176656c2d6174747269627574652d6d6f64656c2d63617374696e67)](https://github.com/kontoulis/laravel-attribute-model-casting/LICENSE)

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [Motivation](#motivation)
- [Installation](#installation)
- [Usage](#usage)
    - [Defining Casts with Attributes](#defining-casts-with-attributes)
    - [Benefits](#benefits)
- [Contributing](#contributing)
- [License](#license)

Introduction
------------

[](#introduction)

This package introduces a `Cast` attribute to enhance type casting for Eloquent model attributes. By leveraging PHP 8 attributes, this package allows you to define casts in a more modern, reusable, and maintainable way, especially when working with traits.

**Note**: This approach can be used together with the existing Eloquent casting system. The `Cast` attribute will be applied in addition to any other casts defined in the model.

Motivation
----------

[](#motivation)

Defining casts in Eloquent models can become repetitive, especially when the same casts are applied across multiple models. This package provides a clean and modern solution by allowing you to define casts using attributes, making it easier to reuse and maintain your code.

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

[](#installation)

You can install the package via Composer:

```
composer require kontoulis/laravel-attribute-model-casting
```

### Prerequisites

[](#prerequisites)

- PHP 8.0 or higher
- Laravel 11 or higher

Usage
-----

[](#usage)

### Defining Casts with Attributes

[](#defining-casts-with-attributes)

You can use the `Cast` attribute to define type casting for model properties.
To resolve the actual casts, the model must use the included `AttributeCasting` trait.

Here's an example:

```
use Kontoulis\LaravelAttributeModelCasting\AttributeCasting;
use Kontoulis\LaravelAttributeModelCasting\Cast;

#[Cast('price', Price::class)]
trait HasPrice {
    public function priceEquals(Price $other): bool
    {
        return $this->amount === $other->amount && $this->currency === $other->currency;
    }
}

#[Cast('normalModelAttribute', 'int')]
class EloquentModelWithAttributeCasts extends Model
{
    use AttributeCasting;
    use HasPrice;
}
```

In this example:

- The `HasPrice` trait defines a `Cast` attribute for the `price` property, allowing it to be reused across multiple models.
- The `EloquentModelWithAttributeCasts` class uses the `Cast` attribute to define a cast for the `normalModelAttribute` property.

### Benefits

[](#benefits)

- **Reusability**: Define casts once in a trait and reuse them across multiple models.
- **Modern Syntax**: Leverage PHP 8 attributes for a cleaner and more expressive syntax.
- **Reduced Redundancy**: No need to manually define the same casts in every model.

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

[](#contributing)

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](https://github.com/kontoulis/laravel-attribute-model-casting/LICENSE).

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance53

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

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

Unknown

Total

1

Last Release

326d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/31dcb4fd58402de973ea2f3f49187cfbe2d1752b262e89b51479f2e10cc27235?d=identicon)[kontoulis](/maintainers/kontoulis)

---

Top Contributors

[![kontoulis](https://avatars.githubusercontent.com/u/6674486?v=4)](https://github.com/kontoulis "kontoulis (3 commits)")

---

Tags

attributescastingcastseloquentlaravelphpphp-attributesphplaravelmodeleloquentattributescasts

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kontoulis-laravel-attribute-model-casting/health.svg)

```
[![Health](https://phpackages.com/badges/kontoulis-laravel-attribute-model-casting/health.svg)](https://phpackages.com/packages/kontoulis-laravel-attribute-model-casting)
```

###  Alternatives

[wendelladriel/laravel-lift

Take your Eloquent Models to the next level

70046.8k](/packages/wendelladriel-laravel-lift)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[sebastiaanluca/laravel-boolean-dates

Automatically convert Eloquent model boolean attributes to dates (and back).

40111.7k1](/packages/sebastiaanluca-laravel-boolean-dates)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3059.0k](/packages/matchory-elasticsearch)[kiwilan/typescriptable-laravel

PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript. If you want to use some helpers with Inertia, you can install associated NPM package.

3920.9k](/packages/kiwilan-typescriptable-laravel)[waad/laravel-model-metadata

A robust Laravel package for handling metadata with JSON casting, custom relation names, and advanced querying capabilities.

823.1k](/packages/waad-laravel-model-metadata)

PHPackages © 2026

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