Skip to main content
Sometimes you want to change the name of a property in the transformed payload. You can do this with attributes:
class ContractData extends Data
{
    public function __construct(
        public string $name,
        #[MapOutputName('record_company')]
        public string $recordCompany,
    ) {
    }
}
Transformed output:
[
    'name' => 'Rick Astley',
    'record_company' => 'RCA Records',
]

Class-Level Mapping

Change all property names in a data object to snake_case:
#[MapOutputName(SnakeCaseMapper::class)]
class ContractData extends Data
{
    public function __construct(
        public string $name,
        public string $recordCompany,
    ) {
    }
}

Input and Output Mapping

Use the MapName attribute to combine input and output property name mapping:
#[MapName(SnakeCaseMapper::class)]
class ContractData extends Data
{
    public function __construct(
        public string $name,
        public string $recordCompany,
    ) {
    }
}

Global Name Mapping

Set a default name mapping strategy for all data objects in config/data.php:
'name_mapping_strategy' => [
    'input' => null,
    'output' => SnakeCaseMapper::class,
],
Now all data objects will automatically map property names to snake_case in the output:
$contract = new ContractData(
    name: 'Rick Astley',
    recordCompany: 'RCA Records',
);

Available Mappers

The package includes several built-in mappers:
  • SnakeCaseMapper - Converts to snake_case
  • CamelCaseMapper - Converts to camelCase
  • StudlyCaseMapper - Converts to StudlyCase
  • KebabCaseMapper - Converts to kebab-case
For a complete list of available property mappers, see the advanced usage documentation.

Build docs developers (and LLMs) love