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 Castable Data Transfer Object

Laravel Castable Data Transfer Object Laravel Package

jessarcher/laravel-castable-data-transfer-object

A Laravel package that lets Eloquent models cast attributes directly to data transfer objects, keeping structured data typed and clean while handling JSON serialization automatically. Ideal for replacing loose arrays with explicit DTO classes.

View on GitHub
Deep Wiki
Context7

Automatically cast JSON columns to rich PHP objects in Laravel using Spatie's data-transfer-object class

Frequently asked questions about Laravel Castable Data Transfer Object
How do I install jessarcher/laravel-castable-data-transfer-object?
Install it with Composer using composer require jessarcher/laravel-castable-data-transfer-object. Laravel package auto-discovery registers the service provider, so manual provider registration is typically unnecessary. After installing, define your DTO classes and reference them in your model's casts. Check that your PHP and Laravel versions satisfy the package's Composer constraints before adopting it.
Which Laravel versions does this package support?
Version 2.3.0 adds Laravel 10.x compatibility, which is the key change in that release. Earlier 2.x releases target prior Laravel versions, so review the composer.json constraints for your exact Laravel and PHP combination. If you are standardizing on Laravel 10, v2.3.0 is the release to use. No breaking changes or deprecations are indicated in the v2.3.0 release notes.
How do I cast a JSON column to a data transfer object in an Eloquent model?
Add your DTO class to the model's casts array, mapping the JSON column name to that class. Eloquent then hydrates the DTO from the stored JSON when the attribute is read. When you assign a DTO instance and save, the package serializes it back to JSON. Keep the DTO's typed properties aligned with the JSON column's expected shape.
Do I need Spatie's data-transfer-object package installed?
Yes. The package is built around Spatie's data-transfer-object class, so your DTOs follow that class's conventions. The casting layer handles converting between the DTO instances and the JSON stored in the database. Install the Spatie version required by this package's Composer constraints to avoid conflicts.
How are null values handled when casting attributes to DTOs?
Null handling depends on your column definition and DTO design, so verify it with model-level tests. A nullable JSON column typically yields null instead of an empty DTO, while malformed or partial data can cause hydration errors. Use nullable properties and sensible defaults in your DTO where appropriate. Test round-trips with null, empty, and missing data for each DTO-backed attribute.
Can I use nested DTOs or arrays of DTOs with this package?
The cast maps a JSON column to a single DTO class, and nested structures depend on how that DTO defines its own properties and casting. Spatie's DTO supports typed properties and nested construction, so you can model nested objects or arrays of DTOs inside your classes. Confirm the exact patterns your team needs with tests, since complex hydration is where edge cases tend to appear. Benchmark high-volume models if you hydrate large nested structures frequently.
Does the package serialize the DTO back to JSON when saving a model?
Yes. The cast works in both directions: JSON from the database becomes a DTO, and a DTO assigned to the attribute is serialized back to JSON on save. That round-trip keeps structured data typed in your application while the column remains plain JSON. Add persistence round-trip tests for each DTO-backed attribute to catch mismatches between the DTO shape and the column format.
Is this package safe to use in production Laravel applications?
Technical risk is low because it is a narrow model-layer utility with a small API surface, and v2.3.0 is a compatibility update with no indicated breaking changes. The main risks are indirect, including reliance on a small community package for persistence and serialization behavior. It is community-supported with no vendor SLA, so assign internal ownership for dependency monitoring and keep a fallback plan if it lags future Laravel releases. Mitigate with model-level tests and a staged rollout.
How do I test DTO casting in my Laravel application?
Write model-level tests that cover DTO hydration from JSON, null and malformed data handling, and persistence round-trips back to the database. Assert that reading the attribute returns the expected DTO type and values, and that saving writes the expected JSON. Run these tests in a branch before expanding DTO usage to core domain models. Include a test for every nullable or optional property your DTOs rely on.
What are alternatives to this package for typed JSON columns in Laravel?
Native Eloquent casts such as array, collection, or custom cast classes can handle JSON columns without an extra dependency. You can also write a custom cast that hydrates your own DTO classes directly. Spatie's laravel-data package is another option for richer DTO and validation features. Choose this package when its lightweight Spatie data-transfer-object style matches your existing conventions.
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.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections