Weave 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 Custom Casts

Laravel Custom Casts Laravel Package

vkovic/laravel-custom-casts

vkovic/laravel-custom-casts provides reusable, class-based custom attribute casts for Laravel Eloquent models, keeping get/set and serialization logic organized, testable, and out of your models—ideal for value objects, arrays, and other complex attribute transformations.

View on GitHub
Deep Wiki
Context7

Make your own custom cast type for Laravel model attributes

Frequently asked questions about Laravel Custom Casts
What is vkovic/laravel-custom-casts used for?
It provides a base abstraction for defining reusable, class-based custom attribute casts on Laravel Eloquent models. Instead of stuffing transformation logic into accessors and mutators, you create dedicated cast classes for get, set, and serialization behavior. It is aimed at value objects, arrays, and other complex attribute transformations. Note that modern Laravel covers the same need natively through the CastsAttributes interface.
How do I install vkovic/laravel-custom-casts?
Install it with Composer using composer require vkovic/laravel-custom-casts. Basic usage does not require publishing configuration or registering a service provider, since the package works through model $casts declarations. Because the last release was in December 2020, pin the version explicitly in composer.json and verify it resolves against your PHP and Laravel constraints before committing.
How do I create a custom cast class and use it on a model attribute?
You define a class that extends the package's cast base class and implement the get and set logic for the attribute. Then you register that class in the model's $casts array, keyed by the attribute name, exactly like a built-in cast. The value is hydrated into your object or structure on access and written back on assignment. Keep cast classes small and focused so they stay testable in isolation.
Which Laravel and PHP versions does this package support?
The package originates from the pre-native-casts era of Laravel, with its last tagged release in December 2020, so it targets older Laravel and PHP 7.x combinations. There is no published, actively maintained compatibility matrix for PHP 8.x or Laravel 10 and 11. You should run your own compatibility test against the exact PHP and Laravel versions in your application before relying on it.
Does this package work with Laravel 10 or Laravel 11?
It may install and function, but compatibility with current Laravel versions is untested by the maintainer given the 2020 release date. Laravel has shipped first-party custom casts since version 7, so there is little reason to carry the dependency forward on Laravel 10 or 11. If you inherit it, run your model, serialization, queue, and API tests against the target framework version before upgrading.
How is this different from Laravel's native custom casts?
Native Laravel casts implement the CastsAttributes or CastsInboundAttributes interfaces and require no third-party base class. This package predates those interfaces and provides its own abstraction layer, which couples your cast classes to a package-specific parent. Functionally the idea is the same, but native casts are framework-supported, maintained, and better documented. For new development, use the native interfaces.
Does it support null values, arrays, JSON serialization, and cast parameters?
The package is designed for complex transformations such as value objects and arrays, but edge-case behavior around nulls, dirty tracking, and serialization must be verified in your own application. Support for cast parameters and inbound-only casting is not documented in a way you should assume, especially on current Laravel versions. Write contract tests covering hydration, persistence, toArray, and toJson before trusting it in production.
Is vkovic/laravel-custom-casts maintained and safe to use in production?
Upstream activity is low, with the last release in December 2020 and limited ecosystem adoption, which creates real long-term maintenance risk. It is reasonable as a time-boxed legacy dependency in an application that already depends on it, but not for greenfield work. The MIT license lets you fork it, though that transfers ongoing ownership to your team. Plan either to freeze, fork, or migrate off it.
How do I migrate from this package to native Laravel casts?
Start by inventorying every cast class and every $casts declaration that references the package. Rewrite each cast as a CastsAttributes or CastsInboundAttributes implementation, preserving the original get and set semantics and null handling exactly. Add contract tests for hydration, persistence, serialization via toArray and toJson, and dirty tracking, then remove the Composer dependency once parity is proven. Migrate high-risk models first and avoid exposing package base classes in public APIs.
What are the alternatives to vkovic/laravel-custom-casts?
The primary alternative is Laravel's built-in custom cast support using CastsAttributes and CastsInboundAttributes, which requires no extra dependency and is actively maintained. For value objects you can also use accessor and mutator methods or a dedicated value-object package alongside native casts. No widely adopted maintained fork of this package is known, so native casts are the safest long-term target and the recommended migration endpoint.
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.
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core
nodisrupt/laravel-agent