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(1y ago)12MITPHP

Since Jun 20Pushed 1y 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 today

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

25

—

LowBetter than 35% of packages

Maintenance49

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity37

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

379d 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

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11223.5M33](/packages/anourvalar-eloquent-serialize)[wendelladriel/laravel-lift

Take your Eloquent Models to the next level

70349.7k](/packages/wendelladriel-laravel-lift)[waad/laravel-model-metadata

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

854.6k](/packages/waad-laravel-model-metadata)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3064.0k](/packages/matchory-elasticsearch)[salehhashemi/laravel-repository

Implementing the repository pattern for Laravel projects.

2010.5k](/packages/salehhashemi-laravel-repository)

PHPackages © 2026

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