Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/spatie/laravel-data/llms.txt

Use this file to discover all available pages before exploring further.

Property mappers automatically transform property names between different naming conventions when creating and transforming data objects.

Available Mappers

The package provides seven built-in mappers:
use Spatie\LaravelData\Mappers\CamelCaseMapper;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
use Spatie\LaravelData\Mappers\KebabCaseMapper;
use Spatie\LaravelData\Mappers\ProvidedNameMapper;
use Spatie\LaravelData\Mappers\StudlyCaseMapper;
use Spatie\LaravelData\Mappers\LowerCaseMapper;
use Spatie\LaravelData\Mappers\UpperCaseMapper;

Usage Example

class ContractData extends Data
{
    public function __construct(
        #[MapName(CamelCaseMapper::class)]
        public string $name,
        #[MapName(SnakeCaseMapper::class)]
        public string $recordCompany,
        #[MapName(KebabCaseMapper::class)]
        public string $vatNumber,
        #[MapName(new ProvidedNameMapper('country field'))]
        public string $country,
        #[MapName(StudlyCaseMapper::class)]
        public string $cityName,
        #[MapName(LowerCaseMapper::class)]
        public string $addressLine1,
        #[MapName(UpperCaseMapper::class)]
        public string $addressLine2,
    ) {
    }
}

Creating from Mapped Names

ContractData::from([
    'name' => 'Rick Astley',
    'record_company' => 'RCA Records',
    'vat-number' => 'BE0123456789',
    'country field' => 'Belgium',
    'CityName' => 'Antwerp',
    'addressline1' => 'some address line 1',
    'ADDRESSLINE2' => 'some address line 2',
]);

Transformation Output

When transforming, the mapped names are used:
{
    "name" : "Rick Astley",
    "record_company" : "RCA Records",
    "vat-number" : "BE0123456789",
    "country field" : "Belgium",
    "CityName" : "Antwerp",
    "addressline1" : "some address line 1",
    "ADDRESSLINE2" : "some address line 2"
}

Mapper Details

Transforms property names to camelCase:
  • propertyNamepropertyName
  • PropertyNamepropertyName
  • property_namepropertyName
Transforms property names to snake_case:
  • propertyNameproperty_name
  • PropertyNameproperty_name
  • property-nameproperty_name
Transforms property names to kebab-case:
  • propertyNameproperty-name
  • PropertyNameproperty-name
  • property_nameproperty-name
Transforms property names to StudlyCase (PascalCase):
  • propertyNamePropertyName
  • property_namePropertyName
  • property-namePropertyName
Transforms property names to lowercase:
  • PropertyNamepropertyname
  • PROPERTYNAMEpropertyname
Transforms property names to UPPERCASE:
  • propertyNamePROPERTYNAME
  • PropertyNamePROPERTYNAME
Maps to a specific name you provide:
#[MapName(new ProvidedNameMapper('custom_field_name'))]
public string $property;

Build docs developers (and LLMs) love