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

Abandoned → [vkovic/laravel-custom-casts](/?search=vkovic%2Flaravel-custom-casts)Library[Database &amp; ORM](/categories/database)

sfneal/laravel-custom-casts
===========================

Make your own custom cast type for Laravel model attributes

v1.5.0(2y ago)01.7k[2 PRs](https://github.com/sfneal/laravel-custom-casts/pulls)MITPHPPHP ^8.0CI passing

Since Dec 5Pushed 3mo agoCompare

[ Source](https://github.com/sfneal/laravel-custom-casts)[ Packagist](https://packagist.org/packages/sfneal/laravel-custom-casts)[ RSS](/packages/sfneal-laravel-custom-casts/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (6)Dependencies (2)Versions (24)Used By (0)

Laravel Custom Casts
====================

[](#laravel-custom-casts)

[![Build](https://camo.githubusercontent.com/34d124b0171fde9136eac214fcbdf2935ec60e8f3f77a87cf4c3ddad84cde792/68747470733a2f2f6170692e7472617669732d63692e6f72672f766b6f7669632f6c61726176656c2d637573746f6d2d63617374732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/vkovic/laravel-custom-casts)[![Downloads](https://camo.githubusercontent.com/5c84046ec95e08266b961e73f5f55916e855d91c3df8b59242e17223084d722c/68747470733a2f2f706f7365722e707567782e6f72672f766b6f7669632f6c61726176656c2d637573746f6d2d63617374732f646f776e6c6f616473)](https://packagist.org/packages/vkovic/laravel-custom-casts)[![Stable](https://camo.githubusercontent.com/8d95644aed399fefa7aa2f952d09bc94a1c3ad4617ed1acde24b3e717910da33/68747470733a2f2f706f7365722e707567782e6f72672f766b6f7669632f6c61726176656c2d637573746f6d2d63617374732f762f737461626c65)](https://packagist.org/packages/vkovic/laravel-custom-casts)[![License](https://camo.githubusercontent.com/92f4528f71e70baea29597b7c1fda0b077b9dcc737805154cc58953500577ab1/68747470733a2f2f706f7365722e707567782e6f72672f766b6f7669632f6c61726176656c2d637573746f6d2d63617374732f6c6963656e7365)](https://packagist.org/packages/vkovic/laravel-custom-casts)

### Make your own cast type for Laravel model attributes

[](#make-your-own-cast-type-for-laravel-model-attributes)

Laravel custom casts works similarly to [Eloquent attribute casting](https://laravel.com/docs/6.x/eloquent-mutators#attribute-casting), but with custom-defined logic (in a separate class). This means we can use the same casting logic across multiple models — we might write [image upload logic](https://github.com/vkovic/laravel-custom-casts/tree/v1.0.2#example-casting-user-image) and use it everywhere. In addition to casting to custom types, this package allows custom casts to listen and react to underlying model events.

Let's review some Laravel common cast types and examples of their usage:

```
namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'is_admin' => 'boolean',
        'login_count' => 'integer'
        'height' => 'decimal:2'
    ];
}
```

In addition to `boolean`, `integer`, and `decimal`, out of the box Laravel supports `real`, `float`, `double`, `string`, `object`, `array`, `collection`, `date`, `datetime`, and `timestamp` casts.

Sometimes it is convenient to handle more complex types with custom logic, and for casts to be able to listen and react to model events. This is where this package come in handy.

> Handling events directly from custom casts can be very useful if, for example, we're storing an image using a custom casts and we need to delete it when the model is deleted. *Check out the [old documentation](https://github.com/vkovic/laravel-custom-casts/tree/v1.0.2#example-casting-user-image) for this example.*

### 📦 vkovic packages 📦

[](#package-vkovic-packages-package)

Please check out my other packages — they are all free, well-written, and some of them are useful 😄. If you find something interesting, consider giving me a hand with package development, suggesting an idea or some kind of improvement, starring the repo if you like it, or simply check out the code - there's a lot of useful stuff under the hood.

- [**vkovic/laravel-commando**](http://bit.ly/2GT7DV7) ~ Collection of useful `artisan` commands
- *Coming soon* [**vkovic/laravel-event-log**](http://bit.ly/2MFtCn8) ~ Easily log and access logged events, optionally with additional data and the related model

Compatibility
-------------

[](#compatibility)

The package is compatible with **Laravel** versions `5.5`, `5.6`, `5.7`, `5.8` and `6`

and **Lumen** versions `5.5`, `5.6`, `5.7`, `5.8`.

Laravel 7+ has native support for [Custom Casts](https://laravel.com/docs/7.x/eloquent-mutators#custom-casts) that are incompatible with this library.

Minimum supported version of PHP is `7.1`. PHP `8` is also supported.

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

[](#installation)

Install the package via Composer:

```
composer require vkovic/laravel-custom-casts
```

Usage
-----

[](#usage)

### Utilizing a custom cast class

[](#utilizing-a-custom-cast-class)

To enable custom casts in a model, use the `HasCustomCasts` trait and define which attributes will be casted using `$casts` - per Laravel standards.

```
// File: app/User.php

namespace App;

use App\CustomCasts\NameCast;
use Illuminate\Database\Eloquent\Model;
use Vkovic\LaravelCustomCasts\HasCustomCasts;

class User extends Model
{
    use HasCustomCasts;

    protected $casts = [
        'is_admin' => 'boolean', //  NameCast::class // getTitle() . ' ' . $value;
    }

    protected function getTitle()
    {
        return ['Mr.', 'Mrs.', 'Ms.', 'Miss'][rand(0, 3)];
    }
}
```

The required `setAttribute` method receives the `$value` being set on the model field, and should return a raw value to store in the database.

The optional `castAttribute` method receives the raw `$value` from the database, and should return a mutated value. If this method is omitted, the raw database value will be returned.

For the sake of this example we'll implement one more method which will attach a random title to a user when their name is retrieved from database.

### Testing a custom cast class

[](#testing-a-custom-cast-class)

Let's create a user and see what happens.

```
$user = new App\User;
$user->name = 'john doe';

$user->save();
```

This will create our new user and store their name in the database, with the first letter of each word uppercased.

When we retrieve the user and try to access their name, title will be prepended to it — just like we defined in our custom `NameCast` class.

```
dd($user->name); // 'Mr. John Doe'
```

### Handling model events

[](#handling-model-events)

Let's say that we want to notify our administrator when a user's name changes.

```
// File: app/CustomCasts/NameCast.php

public function updated()
{
    $attribute = $this->attribute;

    if($this->model->isDirty($attribute)) {
        // Notify admin about name change
    }
}
```

In addition to the `updated` method, we can define other methods for standard model events: `retrieved`, `creating`, `created`, `updating`, `saving`, `saved`, `deleting`, `deleted`, `restoring` and `restored`.

### Other functionality

[](#other-functionality)

As you can see from the above code, we can easily access the casted attribute name as well as an instance of the underlying model.

```
// File: app/CustomCasts/NameCast.php

// Get the name of the model attribute being casted
dd($this->attribute); // 'name'

// Access our `User` model
dd(get_class($this->model)); // 'App/User'
```

We can also retrieve all casted attributes and their corresponding classes directly from the model.

```
// File: app/User.php

dd($this->getCustomCasts()); // ['name' => 'App/CustomCasts/NameCast']
```

### Using aliased casts

[](#using-aliased-casts)

You may find it easier to use aliases for custom casts, e.g.:

```
protected $casts = [
    'avatar' => 'image' //  ImageCast::class //
