Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Json Casted

Laravel Json Casted Laravel Package

novius/laravel-json-casted

Cast JSON columns in Laravel models with per-key casting. Define sub-key casts via a method or dedicated cast class; values are exposed as a Fluent object with proper types (e.g., Carbon dates). Supports PHP 8.2+ and Laravel 10+.

View on GitHub
Deep Wiki
Context7

A package to cast json fields, each sub-keys is castable

Frequently asked questions about Laravel Json Casted
How do I cast specific nested keys in a JSON column (e.g., dates, booleans) in Laravel?
Use `novius/laravel-json-casted` by defining casts in your model’s `$casts` array with `JsonCasted::class:methodName` or a dedicated cast class. For example, cast a `date` key as `date:Y-m-d` in a JSON field named `extras`. The package then returns a Fluent object where `$model->extras->date` is a Carbon instance.
Does this package work with Laravel 10 and PHP 8.2+?
Yes, the package explicitly supports Laravel 10+ and PHP 8.2+. Check the [GitHub repo](https://github.com/novius/laravel-json-casted) for updates, but it’s currently aligned with these versions. Always test in your staging environment before production deployment.
Can I use custom cast types (e.g., UUID, enum) with this package?
Yes, the package supports Laravel’s native cast syntax, so you can use custom casts like `uuid` or `enum` by defining them in your model’s cast method or class. For example, add `'id' => 'uuid'` to your `$casts` array. The Fluent object will handle the conversion automatically.
How do I handle null or missing JSON keys when accessing casted values?
Use Laravel’s `whenFilled()` or the Fluent object’s `get()` method with a default value. For example, `$model->extras->get('date', default: null)` prevents errors if the key is missing. Alternatively, add validation in model observers to ensure required keys exist before casting.
Will this package slow down my application if I cast large JSON fields?
Casting introduces overhead because each access triggers Laravel’s casting pipeline. For large JSON payloads, consider lazy-loading sub-fields with custom accessors or offloading casting to the database (e.g., PostgreSQL’s `jsonb` operators). Benchmark performance in your staging environment.
How do I integrate this with `laravel-ide-helper` for better autocompletion?
Add `ModelHasJsonWithCastsHook` to your `laravel-ide-helper` configuration under `model_hooks`. This generates PHPDoc blocks for your JSON-casted fields, enabling IDE autocompletion (e.g., `$model->extras->date` will show as a Carbon instance).
What happens if the JSON data is malformed or invalid?
The package will throw exceptions or fail silently depending on the error. Add validation in model observers or accessors to catch issues early. For example, use `json_validate()` in a `saving` observer to reject malformed JSON before casting.
Can I use this package with Laravel’s API Resources or Serialization?
Yes, the Fluent object returned by the package integrates seamlessly with API Resources. Access casted values directly in your resource (e.g., `$model->extras->date`) or use the `Resource`’s `mergeWhen` method to include only specific keys.
Are there alternatives to this package for JSON casting in Laravel?
Alternatives include Laravel’s built-in `array` cast (limited to top-level keys) or third-party packages like `spatie/laravel-json-attributes`. However, `novius/laravel-json-casted` uniquely supports per-key casting with Fluent access and integrates with Laravel’s native casting system.
How do I debug issues with casting or Fluent object access?
Enable Laravel’s debug mode and check for exceptions when accessing nested keys. Use `dd($model->extras)` to inspect the Fluent object’s structure. For logging, integrate Laravel’s `Log::error()` in custom cast classes or model observers to track casting failures.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony
spatie/flare-daemon-runtime