PHPackages                             slruslan/laravel-eloquent-custom-casts - 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. slruslan/laravel-eloquent-custom-casts

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

slruslan/laravel-eloquent-custom-casts
======================================

Provides Laravel Eloquent trait to enable custom class object casting.

1.0.2(9y ago)51711GPLPHPPHP &gt;=5.6

Since Apr 17Pushed 9y ago3 watchersCompare

[ Source](https://github.com/slruslan/laravel-eloquent-custom-casts)[ Packagist](https://packagist.org/packages/slruslan/laravel-eloquent-custom-casts)[ Docs](https://github.com/slruslan/laravel-eloquent-custom-casts)[ RSS](/packages/slruslan-laravel-eloquent-custom-casts/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (3)DependenciesVersions (3)Used By (0)

Laravel Eloquent custom casts trait
===================================

[](#laravel-eloquent-custom-casts-trait)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f68b3e21e36de546902ad6c5b4c18578e6e0b4cdfb0e26273b410657f5f860c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736c7275736c616e2f6c61726176656c2d656c6f7175656e742d637573746f6d2d63617374732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/slruslan/laravel-eloquent-custom-casts)[![License GPL](https://camo.githubusercontent.com/eeaed0629e9b1fb61eba86008d9f88253778639385cfb96e631d554f9cdd2a63/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d47504c2d626c75652e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/eeaed0629e9b1fb61eba86008d9f88253778639385cfb96e631d554f9cdd2a63/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d47504c2d626c75652e7376673f7374796c653d666c61742d737175617265)

Laravel Eloquent provides an Attribute Casting function, that allows you to automatically convert attributes to common data types. By default, supported cast types are: `integer, real, float, double,  string, boolean, object, array, collection, date, datetime, and timestamp.`

As you can see, custom class types are not supported. This trait adds this support, so you automatically serialize and deserialize your custom classes.

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

[](#installation)

Using Composer:

```
$ composer require slruslan/laravel-eloquent-custom-casts --dev
```

Usage
-----

[](#usage)

1. Create a field of TEXT type where your data will be stored.
2. To enable trait, add `use Slruslan\CustomCasts\CustomCasts;` line to your Eloquent Model class.

For example:

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Slruslan\CustomCasts\CustomCasts;

class Post extends Model
{
    use CustomCasts;
```

3. Add your fields and class names to be serialized in protected $casts array.

```
    protected $casts = [
        'geo_location' => \App\Services\CustomLocation::class,
        'another_custom_field' => \App\Services\AnotherCustomField::class
    ];
```

4. You're ready to use your model. Here is a basic example:

```
    $post = Post::find(1);

    // For example, imagine we have an \App\Services\CustomLocation class,
    // that implements any custom logics and for some reasons
    // we have to store it in DB as it is.

    // Let's instantinate it.
    $location = new \App\Services\CustomLocation(55.9937441, 92.7521816);

    // And call some methods, imagine we have them there.
    $location->updatePosition();
    $location->callAPI();

    // After that, we want to save it in our post model.
    // We can just assign the value and call default save() method.
    $post->geo_location = $location;
    $post->save();

    // It's saved. Let's get a post again
    // and check the field class.

    $post = Post::find(1);

    var_dump($post instanceof \App\Services\CustomLocation);

    // Outputs:
    // bool(true)
```

License
-------

[](#license)

GNU General Public License v3.0 (GPL). Refer to the [LICENSE](LICENSE) file to get more info.

Author contacts:
----------------

[](#author-contacts)

You can ask me any questions by:

Email:

VK: [vk.com/slruslan](https://vk.com/slruslan)

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

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

Total

2

Last Release

3329d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5718556?v=4)[Ruslan](/maintainers/slruslan)[@slruslan](https://github.com/slruslan)

---

Top Contributors

[![slruslan](https://avatars.githubusercontent.com/u/5718556?v=4)](https://github.com/slruslan "slruslan (6 commits)")

### Embed Badge

![Health badge](/badges/slruslan-laravel-eloquent-custom-casts/health.svg)

```
[![Health](https://phpackages.com/badges/slruslan-laravel-eloquent-custom-casts/health.svg)](https://phpackages.com/packages/slruslan-laravel-eloquent-custom-casts)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M117](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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