PHPackages                             jn-jairo/laravel-cast - 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. jn-jairo/laravel-cast

ActiveLibrary

jn-jairo/laravel-cast
=====================

Cast for Laravel.

v2.0.2(1y ago)23041MITPHPPHP ^8.1

Since Sep 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/jn-jairo/laravel-cast)[ Packagist](https://packagist.org/packages/jn-jairo/laravel-cast)[ Docs](https://github.com/jn-jairo/laravel-cast)[ RSS](/packages/jn-jairo-laravel-cast/feed)WikiDiscussions 2.x Synced yesterday

READMEChangelog (10)Dependencies (8)Versions (16)Used By (1)

[![Total Downloads](https://camo.githubusercontent.com/82631d449045fd3a423220cf9d4e296282c754280f4f0ece0194870740ab51db/68747470733a2f2f706f7365722e707567782e6f72672f6a6e2d6a6169726f2f6c61726176656c2d636173742f646f776e6c6f616473)](https://packagist.org/packages/jn-jairo/laravel-cast)[![Latest Stable Version](https://camo.githubusercontent.com/58477bfcfce7f0a593f9934decbdde2f1275af4fc57419a907a222b5168f863e/68747470733a2f2f706f7365722e707567782e6f72672f6a6e2d6a6169726f2f6c61726176656c2d636173742f762f737461626c65)](https://packagist.org/packages/jn-jairo/laravel-cast)[![License](https://camo.githubusercontent.com/86c857c9a45b3c3fe6db8a1ec42fde6635a2bf069b0465841404e2d770735bf5/68747470733a2f2f706f7365722e707567782e6f72672f6a6e2d6a6169726f2f6c61726176656c2d636173742f6c6963656e7365)](https://packagist.org/packages/jn-jairo/laravel-cast)

Cast for Laravel
================

[](#cast-for-laravel)

This package provide cast for Laravel.

Version Compatibility
---------------------

[](#version-compatibility)

LaravelEloquent Cast5.8.x1.x6.x1.x7.x1.x8.x2.x9.x2.x10.x2.x11.x2.x12.x2.xInstallation
------------

[](#installation)

You can install the package via composer:

```
composer require jn-jairo/laravel-cast
```

The `CastServiceProvider` will be automatically registered for you.

Usage
-----

[](#usage)

Both [contract](https://laravel.com/docs/contracts) `\JnJairo\Laravel\Cast\Contracts\Cast` and [facade](https://laravel.com/docs/facades) `\JnJairo\Laravel\Cast\Facades\Cast` are available.

There are three methods `Cast::cast()`, `Cast::castDb()` and `Cast::castJson()`.

- `Cast::cast()` casts to `PHP` types
- `Cast::castDb()` casts to `database` types
- `Cast::castJson()` casts to `json` types

All methods accept three parameters `mixed $value`, `string $type` and optionally `string $format`.

Examples
--------

[](#examples)

```
print_r(Cast::cast('{"foo":"bar"}', 'array'));
/*
Array
(
    [foo] => bar
)
*/

print_r(Cast::castDb(1234.555, 'decimal', '10:2'));
// 1234.56

print_r(Cast::castDb(['foo' => 'bar'], 'json'));
// {"foo":"bar"}

print_r(Cast::castJson(new DateTime('01 jan 2000'), 'date'));
// 2000-01-01
```

Types available
---------------

[](#types-available)

- `int`, `integer`
- `float`, `real`, `double`
- `decimal`
- `bool`, `boolean`
- `date`
- `datetime`
- `timestamp`
- `json`
- `array`
- `object`
- `collection`
- `string`, `text`
- `uuid`
- `encrypted`
- `compressed`
- `base64`
- `pipe`
- `enum`

Format parameter
----------------

[](#format-parameter)

- **decimal** - `precision:places,(up|down|ceiling|floor|half_up|half_down|half_even|half_odd|truncate)`. Example: `10:2,half_up`, `10:2`, `2`, `half_up`. Default: `28:2,half_up`. The decimal type uses the  extension, to use this type run `composer require php-decimal/php-decimal:^1.1` and install the decimal extension.
- **date** - Example: `Y-m-d`. Default: `Y-m-d`.
- **datetime**, **timestamp** - Example: `Y-m-d H:i:s`. Default: `Y-m-d H:i:s`.
- **uuid** - `(uuid1|uuid4|ordered)`. Example: `uuid1`. Default: `uuid4`. Empty string value will return a new UUID. To use ordered UUID format run `composer require moontoast/math:^1.1`.
- **encrypted** - `(one|all),base64:key,cipher`. Empty key and cipher uses the laravel configuration `app.key` and `app.cipher`. Example: `base64:N38XBrdzg505959nDEqefo6fNpeTmGy0wTBHRSxrpcQ=,aes-256-cbc`. Default: `one`.
    - `one` - allow double encryption, decrypts only one time.
    - `all` - prevents double encryption, decrypts recursively all encryptions.

```
$decrypted = Cast::cast($encrypted, 'encrypted');
$encrypted = Cast::castDb($decrypted, 'encrypted');
$decrypted = Cast::castJson($encrypted, 'encrypted');
```

- **compressed** - `(always|smaller),(one|all),level,(raw|deflate|gzip)`. Example: `smaller,all,9,gzip`. Default: `always,one,-1,raw`.
    - `always` - always uses the result of compression even if the result is bigger than the original value.
    - `smaller` - only uses the result of compression if the result is smaller than the original value.
    - `one` - allow double compression, decompresses only one time.
    - `all` - prevents double compression, decompresses recursively all compressions.
    - level - level of compression, from `0` (no compression) to `9` (maximum compression). If `-1` is used, the default compression of the zlib library is used.
    - `raw` - uses the encoding `ZLIB_ENCODING_RAW` with `gzdeflate` and `gzinflate`.
    - `deflate` - uses the encoding `ZLIB_ENCODING_DEFLATE` with `gzcompress` and `gzuncompress`.
    - `gzip` - uses the encoding `ZLIB_ENCODING_GZIP` with `gzencode` and `gzdecode`.

```
$decompressed = Cast::cast($compressed, 'compressed');
$compressed = Cast::castDb($decompressed, 'compressed');
$decompressed = Cast::castJson($compressed, 'compressed');
```

- **base64** - `(one|all),prefix`. Example: `base64:`. Default: `one`.
    - `one` - allow double encoding, decodes only one time.
    - `all` - prevents double encoding, decodes recursively all encodings (requires prefix).

```
$decoded = Cast::cast('base64:Rm9vQmFy', 'base64', 'base64:'); // FooBar
$encoded = Cast::castDb('FooBar', 'base64', 'base64:'); // base64:Rm9vQmFy
$decoded = Cast::castJson('base64:Rm9vQmFy', 'base64', 'base64:'); // FooBar
```

- **pipe** - `|type:format|type:format|...|direction`. Example: `|encrypted|compressed|array|`. Default: `||php:>,db:`.
    - The direction format is `>||||` follows from left to right and `|db:`
        `:encrypted:decimal|2:php|>,db|`

```
$array = Cast::cast('q1ZKy89XslJKSixSqgUA', 'pipe', '|base64|compressed|array|'); // ['foo' => 'bar']
$base64Compressed = Cast::castDb(['foo' => 'bar'], 'pipe', '|base64|compressed|array|'); // q1ZKy89XslJKSixSqgUA
$array = Cast::castJson('q1ZKy89XslJKSixSqgUA', 'pipe', '|base64|compressed|array|'); // ['foo' => 'bar']
```

- **enum** - Example: `MyEnum::class`. Default: ` `.

```
enum MyEnum : int
{
    case foo = 1;
    case bar = 2;
}

Cast::cast(1, 'enum', MyEnum::class); // MyEnum::foo
Cast::castDb(MyEnum::foo, 'enum', MyEnum::class); // 1
Cast::castJson(MyEnum::foo, 'enum', MyEnum::class); // 1
```

It can be a instance of `\Illuminate\Contracts\Support\Arrayable` or `\Illuminate\Contracts\Support\Jsonable`.

```
use Illuminate\Contracts\Support\Arrayable;

enum MyEnum : string implements Arrayable
{
    case foo = 1;
    case bar = 2;

    public function description() : string
    {
        return match ($this) {
            self::foo => 'foo description',
            self::bar => 'bar description',
        };
    }

    public function toArray()
    {
        return [
            'name' => $this->name,
            'value' => $this->value,
            'description' => $this->description(),
        ];
    }
}

Cast::cast(1, 'enum', MyEnum::class); // MyEnum::foo
Cast::castDb(MyEnum::foo, 'enum', MyEnum::class); // 1
Cast::castJson(MyEnum::foo, 'enum', MyEnum::class);
// [
//      'name' => 'foo',
//      'value' => 1,
//      'description' => 'foo description'
// ]
```

Custom types
------------

[](#custom-types)

To create a custom type just implements the contract `\JnJairo\Laravel\Cast\Contracts\Type`.

```
class CustomType implements \JnJairo\Laravel\Cast\Contracts\Type
{
    // ...
}
```

Or extends the `\JnJairo\Laravel\Cast\Types\Type` abstract class.

```
class CustomType extends \JnJairo\Laravel\Cast\Types\Type
{
    // ...
}
```

Publish the configuration to `config/cast.php`.

```
php artisan vendor:publish --provider=JnJairo\\Laravel\\Cast\\CastServiceProvider
```

Set the new type in the configuration.

```
// config/cast.php

return [
    'types' => [
        'custom_type' => CustomType::class,
    ],
];
```

And the custom type will be available.

```
Cast::cast('foo', 'custom_type');
```

License
-------

[](#license)

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

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance44

Moderate activity, may be stable

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity75

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

Recently: every ~182 days

Total

15

Last Release

440d ago

Major Versions

v0.0.3 → v1.0.02019-12-26

v1.0.6 → v2.0.02023-03-02

1.x-dev → v2.0.12024-03-14

PHP version history (4 changes)v0.0.1PHP ^7.1

v0.0.2PHP ^7.2

v1.0.5PHP ^7.2|^8.0

v2.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/31b281cc6941f9371b89b557610b84336a1ed73ecc661ee77f490a1abcdcdb9f?d=identicon)[jn-jairo](/maintainers/jn-jairo)

---

Top Contributors

[![jn-jairo](https://avatars.githubusercontent.com/u/5104869?v=4)](https://github.com/jn-jairo "jn-jairo (29 commits)")

---

Tags

jn-jairoLaravel Cast

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/jn-jairo-laravel-cast/health.svg)

```
[![Health](https://phpackages.com/badges/jn-jairo-laravel-cast/health.svg)](https://phpackages.com/packages/jn-jairo-laravel-cast)
```

###  Alternatives

[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.2k84.2M225](/packages/laravel-horizon)[spatie/laravel-honeypot

Preventing spam submitted through forms

1.6k6.0M60](/packages/spatie-laravel-honeypot)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[laravel-notification-channels/apn

Apple APN Push Notification Channel

2021.9M4](/packages/laravel-notification-channels-apn)[genealabs/laravel-governor

Managing policy and control in Laravel.

201262.8k](/packages/genealabs-laravel-governor)[specialtactics/l5-api

Dependencies for the Laravel API Boilerplate package

3672.8k2](/packages/specialtactics-l5-api)

PHPackages © 2026

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