PHPackages                             gregoriohc/laravel-castable - 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. gregoriohc/laravel-castable

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

gregoriohc/laravel-castable
===========================

Laravel package that adds custom casts for models attributes

v3.0.0(4y ago)01761MITPHPPHP ~5.6|~7.0|~8.0

Since Jan 8Pushed 4y ago1 watchersCompare

[ Source](https://github.com/gregoriohc/laravel-castable)[ Packagist](https://packagist.org/packages/gregoriohc/laravel-castable)[ Docs](https://github.com/gregoriohc/laravel-castable)[ RSS](/packages/gregoriohc-laravel-castable/feed)WikiDiscussions master Synced today

READMEChangelog (7)Dependencies (6)Versions (9)Used By (0)

Laravel Castable
================

[](#laravel-castable)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c3c4c2a1b416849f647b5020695a981028297cdc5eb4e5a76a1bef7feb0b3c45/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f677265676f72696f68632f6c61726176656c2d6361737461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gregoriohc/laravel-castable)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/02f5e0c9cec8b5c8260170ef185b4b1e6956f92b9242126d69e153a1a5ebfcf0/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f677265676f72696f68632f6c61726176656c2d6361737461626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/gregoriohc/laravel-castable)[![Coverage Status](https://camo.githubusercontent.com/29f3dca0c3e885e8eb2a53c32b5199c08b7e52c1eca5f5c76bc72839e1bb435b/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f677265676f72696f68632f6c61726176656c2d6361737461626c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/gregoriohc/laravel-castable/code-structure)[![Quality Score](https://camo.githubusercontent.com/1d4b2581b103342dee0a468a0fe1e17f87f9dc930e3cfb0b9142bc5a87e85140/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f677265676f72696f68632f6c61726176656c2d6361737461626c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/gregoriohc/laravel-castable)[![Total Downloads](https://camo.githubusercontent.com/3b55ae8238a20ebc5fd27eb4b3428a40a0e9bc20ba7b8ce7b773f477a8d9e3de/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f677265676f72696f68632f6c61726176656c2d6361737461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gregoriohc/laravel-castable)

Laravel package that adds custom casts for models attributes.

Install
-------

[](#install)

Via Composer

```
$ composer require gregoriohc/laravel-castable
```

In Laravel 5.5 the package will autoregister the service provider. In Laravel 5.4 and before you must install this service provider.

```
// config/app.php
'providers' => [
    ...
    Gregoriohc\Castable\ServiceProvider::class,
    ...
];
```

Usage
-----

[](#usage)

### With Castable base model

[](#with-castable-base-model)

The fastest way is using `\Gregoriohc\Castable\CustomCastableModel` as your base model.

### Without Castable base model

[](#without-castable-base-model)

Add `HasCustomCasts` trait to your the model you want to add custom attribute casting (if you want to use it in all your models, I suggest adding it to your own base model).

Override `castAttribute` model method:

```
protected function castAttribute($key, $value)
{
    return $this->customCastAttribute($key, parent::castAttribute($key, $value));
}
```

Override `setAttribute` model method:

```
public function setAttribute($key, $value)
{
    return parent::setAttribute($key, $value)->customSetAttribute($key, $value);
}
```

Override `toArray` model method:

```
public function toArray()
{
    return $this->customToArray(parent::toArray());
}
```

Add custom casted attributes to the model casts array:

```
protected $casts = [
    'location' => 'point',
    'bounding_box' => 'multipoint',
];
```

A full model example would look like this:

```
namespace App\Models;

use Gregoriohc\Castable\HasCustomCasts;

class Place extends \Illuminate\Database\Eloquent\Model
{
    use HasCustomCasts;

    protected $casts = [
        'location' => 'point',
        'bounding_box' => 'multipoint',
    ];

    protected function castAttribute($key, $value)
    {
        return $this->customCastAttribute($key, parent::castAttribute($key, $value));
    }

    public function setAttribute($key, $value)
    {
        return parent::setAttribute($key, $value)->customSetAttribute($key, $value);
    }

    public function toArray()
    {
        return $this->customToArray(parent::toArray());
    }
}
```

### Attribute migration, setting and getting

[](#attribute-migration-setting-and-getting)

Depending on the custom caster, the attribute will accept and return different values when setting/getting. Also, the required database migration type will differ. For the included casters, you can see the doc in the caster class file.

For example, for the point caster requires a `Point` database migration type, and to set its value you can do the following:

```
$place->location = [12.345, 67.890];
```

### Configuration

[](#configuration)

You can optionally publish the config file with:

```
$ php artisan vendor:publish --provider="Gregoriohc\Castable\ServiceProvider" --tag="config"
```

### Creating a custom caster

[](#creating-a-custom-caster)

You can create a custom caster extending the `\Gregoriohc\Castable\Casters\Caster` class and implementing `as` and `from` methods. For example:

```
namespace \App\Casters;

class SerializableObject extends \Gregoriohc\Castable\Casters\Caster
{
    public function as($value)
    {
        return unserialize($value);
    }

    public function from($value)
    {
        return serialize($value);
    }
}
```

The `as` method must transform the raw attribute value (from the database or internal) to the usable model attribute, and the `from` method must do the opposite thing.

After that, add the custom caster to the config file:

```
// config/castable.php
'casters' => [
    ...
    'serializable' => \App\Casters\SerializableObject::class,
    ...
];
```

Testing
-------

[](#testing)

```
$ composer test
```

Change log
----------

[](#change-log)

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

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Socialware
----------

[](#socialware)

You're free to use this package, but if it makes it to your production environment I highly appreciate you sharing it on any social network.

Credits
-------

[](#credits)

- [Gregorio Hernández Caso](https://github.com/gregoriohc)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity73

Established project with proven stability

 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

Every ~208 days

Recently: every ~309 days

Total

7

Last Release

1798d ago

Major Versions

v1.0.2 → v2.0.12018-01-21

v2.0.3 → v3.0.02021-06-08

PHP version history (3 changes)v1.0.0PHP &gt;=7.0.0

v1.0.2PHP ~5.6|~7.0

v3.0.0PHP ~5.6|~7.0|~8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/fb58cf0b862c6a82caafd070f85e9198c96205a10e77d6cf828bf981be47a0e4?d=identicon)[gregoriohc](/maintainers/gregoriohc)

---

Top Contributors

[![gregoriohc](https://avatars.githubusercontent.com/u/566841?v=4)](https://github.com/gregoriohc "gregoriohc (17 commits)")

---

Tags

laravelmodelattributespropertiescastgregoriohccastable

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/gregoriohc-laravel-castable/health.svg)

```
[![Health](https://phpackages.com/badges/gregoriohc-laravel-castable/health.svg)](https://phpackages.com/packages/gregoriohc-laravel-castable)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

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

591444.8k2](/packages/spiritix-lada-cache)[wendelladriel/laravel-lift

Take your Eloquent Models to the next level

70046.8k](/packages/wendelladriel-laravel-lift)[pdphilip/elasticsearch

An Elasticsearch implementation of Laravel's Eloquent ORM

145360.2k4](/packages/pdphilip-elasticsearch)

PHPackages © 2026

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