PHPackages                             oguzhankrcb/auto-casting-json-resource - 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. [Templating &amp; Views](/categories/templating)
4. /
5. oguzhankrcb/auto-casting-json-resource

ActiveLibrary[Templating &amp; Views](/categories/templating)

oguzhankrcb/auto-casting-json-resource
======================================

This Laravel package automatically casts your JsonResource data using the casting functions you have defined before.

v0.0.1(3y ago)112[2 PRs](https://github.com/oguzhankrcb/auto-casting-json-resource/pulls)MITPHPPHP ^8.0

Since Sep 14Pushed 2y ago2 watchersCompare

[ Source](https://github.com/oguzhankrcb/auto-casting-json-resource)[ Packagist](https://packagist.org/packages/oguzhankrcb/auto-casting-json-resource)[ Docs](https://github.com/oguzhankrcb/auto-casting-json-resource)[ RSS](/packages/oguzhankrcb-auto-casting-json-resource/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (7)Versions (4)Used By (0)

This Laravel package automatically casts your JsonResource data using the casting functions you have defined before.
====================================================================================================================

[](#this-laravel-package-automatically-casts-your-jsonresource-data-using-the-casting-functions-you-have-defined-before)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d4224aa34aa454f316ce29b0ce839ca3f9e546dd986726450676d2726c5eab46/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f67757a68616e6b7263622f6175746f2d63617374696e672d6a736f6e2d7265736f757263652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/oguzhankrcb/auto-casting-json-resource)[![GitHub Tests Action Status](https://camo.githubusercontent.com/3c417ad178c5a4bcde9ba71cf0da54bb1506d435d590b3c093b60e0a50833f4d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6f67757a68616e6b7263622f6175746f2d63617374696e672d6a736f6e2d7265736f757263652f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/oguzhankrcb/auto-casting-json-resource/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/7ab332e9454d7e28cf812c3ee99be8b93666654d341f0f0b279cf22a9aaa63d3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6f67757a68616e6b7263622f6175746f2d63617374696e672d6a736f6e2d7265736f757263652f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/oguzhankrcb/auto-casting-json-resource/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)

This package makes easier to add global castings to your `JsonResource` files

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

[](#installation)

You can install the package via composer:

```
composer require oguzhankrcb/auto-casting-json-resource
```

Usage
-----

[](#usage)

Just add this trait to your `JsonResource`

```
use AutoCastingJsonResource;
```

And then you can cast whatever you want in `casts` array

```
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return $this->autoCast(parent::toArray($request));
    }

    public function casts(): array
    {
        return [
            'integer' => fn ($value) => (int) ($value / 100), // It will divide all integer objects with 100
            Money::class => fn (Money $value) => $value->getMinorAmount()->toInt() / 2, // It will cast all Brick\Money\Money objects to integer and divide them with 2
        ];
    }
```

You can exclude columns if you want (`id` column is excluded by default)

```
    public function excludedColumns(): array
    {
        return [
            'id', // id column will be excluded from castings
        ];
    }
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Oğuzhan KARACABAY](https://github.com/oguzhankrcb)
- [Emre Deligöz](https://github.com/deligoez)
- [Turan Karatuğ](https://github.com/tkaratug)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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

1389d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9517bc58310e283693396c5ed6a1c7c5bbc7aa1121e62012a1abe1dd2ecf64db?d=identicon)[oguzhankrcb](/maintainers/oguzhankrcb)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (7 commits)")[![oguzhankrcb](https://avatars.githubusercontent.com/u/7572058?v=4)](https://github.com/oguzhankrcb "oguzhankrcb (5 commits)")

---

Tags

laraveloguzhankrcbauto-casting-json-resource

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/oguzhankrcb-auto-casting-json-resource/health.svg)

```
[![Health](https://phpackages.com/badges/oguzhankrcb-auto-casting-json-resource/health.svg)](https://phpackages.com/packages/oguzhankrcb-auto-casting-json-resource)
```

###  Alternatives

[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[slimani/filament-media-manager

A media manager plugin for Filament.

126.9k](/packages/slimani-filament-media-manager)

PHPackages © 2026

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