Carbon or Laravel Models need extra attention.
Local Transformers
When you want to transform a specific property, use an attribute with the transformer:DateTimeInterfaceTransformer is shipped with the package and transforms Carbon, CarbonImmutable, DateTime and DateTimeImmutable to a string.
Custom Format
Define a custom format:Built-in Transformers
The package ships with several transformers:- DateTimeInterface
- Arrayable
Transforms date/time objects to strings:Configure the format in
config/data.php or pass it directly:Global Transformers
Global transformers are defined inconfig/data.php and used when no local transformer is specified:
- A specific implementation (e.g.
CarbonImmutable) - An interface (e.g.
DateTimeInterface) - A base class (e.g.
Enum)
Creating Custom Transformers
Create a custom transformer by implementing theTransformer interface:
Without Transforming
Get an array representation without transforming properties:Carbon objects won’t be transformed into strings, and nested data objects won’t be transformed into arrays.
Advanced Transform Method
Thetransform method is highly configurable:
Disable Value Transformation
Disable Property Name Mapping
Enable Wrapping
Add Global Transformers
Transformation Depth
Prevent infinite loops in nested data structures by setting a maximum depth:Global Configuration
Inconfig/data.php: