- What does genealabs/laravel-two-way-attribute-casting do?
- It adds two-way attribute casting to Laravel Eloquent models, so attribute values are transformed both when they are read from the database and when they are saved back. This is useful for custom value objects and consistent transformations that should survive round trips to the database. It is a Composer package for Laravel applications that rely on Eloquent.
- How do I install genealabs/laravel-two-way-attribute-casting with Composer?
- Install it with Composer using composer require genealabs/laravel-two-way-attribute-casting. Laravel package discovery should register the service provider automatically; if you are on an older Laravel version, add the provider to config/app.php manually. After installation, configure the cast on the Eloquent models where you need two-way behavior.
- How is this different from Laravel's standard attribute casting?
- Laravel's standard casts already convert many primitive values on read and, for custom cast classes, on write. This package focuses on two-way casting for Eloquent attributes so the same transformation is applied when saving models, which helps with custom value objects. Use it when you need consistent read and write transformations without duplicating conversion logic in your models.
- Can I use two-way attribute casting with custom value objects in Eloquent?
- Yes. The package is intended for attributes that map to custom value objects and need conversion in both directions. You define the cast on the model and the package converts the database value into your object on read and back into a storable value on save. This keeps value-object handling consistent across model loading and persistence.
- Which Laravel and PHP versions are supported?
- Exact Laravel and PHP constraints are declared in the package's composer.json and on its Packagist page, so check those before installing in an existing application. The package targets Laravel applications that use Eloquent attribute casting. If you are upgrading Laravel, confirm compatibility with your framework version before deploying.
- How do I configure a model to use two-way attribute casting?
- Add the package's two-way cast class to the model's $casts array for the attribute you want to transform. The underlying database column should store a value the cast can convert back into your value object. Then read and save the model as usual; the cast handles conversion in both directions.
- Does it add extra database queries when saving models?
- Attribute casting is handled in PHP during Eloquent hydration and persistence, so the package should not add extra database queries by itself. Any database cost comes from your model's normal insert or update operations. As with any cast, test critical write paths to confirm behavior and performance.
- How should I test two-way attribute casting in a Laravel application?
- Create a model with the two-way cast, save it through Eloquent, and assert that the raw database value is stored in the expected format. Then refresh or retrieve the model and assert that the attribute is hydrated as your value object. Use model factories or direct model creation, and include edge cases such as null values and invalid input.
- Is this package production-ready for saving Eloquent models?
- The package is designed to perform casting during normal Eloquent saves, but you should evaluate its release history, test coverage, and compatibility with your Laravel version before using it in production. Run your application's model tests against a staging database to verify read and write round trips. If your models rely on complex casts, confirm the package handles your specific value objects.
- What are alternatives to genealabs/laravel-two-way-attribute-casting?
- Laravel's built-in custom cast classes implementing CastsAttributes are the primary alternative and support both getting and setting attribute values. You can also handle transformations in model accessors and mutators or use value-object libraries with their own Eloquent integration. This package is an option when you want dedicated two-way casting behavior without writing custom cast logic for each attribute.