Looking to hire Laravel developers? Try LaraJobs

laravel-data-definition maintained by keaton-php

Description
Immutable array wrappers for Laravel: short aliases for deep payload paths, Laravel-native casts, nothing writable.
Author
Last update
2026/11/25 03:32 (1.x-dev)
License
Downloads
0

Comments
comments powered by Disqus

Laravel Data Definition

Latest Version on Packagist Tests Total Downloads

Laravel Data Definition wraps an array payload, like a webhook body or an API response, in a small immutable object. You name the deep paths and their types once, and a value of the wrong type throws an exception. It is never quietly turned into something else.

Say a lead comes in from a webhook, and you need four values from its payload. The usual way is to read each one by hand and cast it:

<?php

use Illuminate\Support\Carbon;

$payload = [
    'created_time' => '2026-03-02T09:15:00Z',
    'campaign' => ['utm_source' => 'google'],
    'contact' => ['fields' => [
        'email' => 'ana@example.com',
        'monthly_budget' => 'n/a',
    ]],
];

$email = $payload['contact']['fields']['email'];
$source = $payload['campaign']['utm_source'];
$at = Carbon::parse($payload['created_time']);
$budget = (int) $payload['contact']['fields']['monthly_budget'];

echo "{$email} via {$source} on {$at->format('j M Y')}", PHP_EOL;
echo "budget: {$budget}", PHP_EOL;
ana@example.com via google on 2 Mar 2026
budget: 0

That reads the payload. But this lead typed n/a for the budget, and (int) turned it into 0. Nothing failed, so that zero is what gets saved.

Here's the same payload read with the package. You write a small class once. $aliases gives each deep path a short name, and $casts says what type it should be:

<?php

use Keaton\DataDefinition\DataDefinition;

class Lead extends DataDefinition
{
    protected array $aliases = [
        'email'  => 'contact.fields.email',
        'budget' => 'contact.fields.monthly_budget',
        'source' => 'campaign.utm_source',
        'at'     => 'created_time',
    ];

    protected array $casts = [
        'budget' => 'int',
        'at'     => 'datetime',
    ];
}

Then you read the lead by those names:

<?php

use Keaton\DataDefinition\Exceptions\CastFailed;

$payload = [
    'created_time' => '2026-03-02T09:15:00Z',
    'campaign' => ['utm_source' => 'google'],
    'contact' => ['fields' => [
        'email' => 'ana@example.com',
        'monthly_budget' => 'n/a',
    ]],
];

$lead = Lead::make($payload);

echo "{$lead->email} via {$lead->source} on {$lead->at->format('j M Y')}", PHP_EOL;

try {
    echo "budget: {$lead->budget}", PHP_EOL;
} catch (CastFailed $e) {
    echo $e->getMessage(), PHP_EOL;
}
ana@example.com via google on 2 Mar 2026
Lead: cannot cast 'budget' to int: got 'n/a' (expected an integer or a plain integer string).

The first line is the same. The budget wasn't a number, so the package threw an exception that names the field and the value, and there is no zero to save. The paths and the types now live in one class, so every part of your app reads a lead the same way.

Typed values and copies

With a number in the budget you get an integer back, and at is a Carbon instance. A definition can't be changed after it is made, but with() returns a changed copy.

<?php

$lead = Lead::make([
    'created_time' => '2026-03-02T09:15:00Z',
    'contact' => ['fields' => ['monthly_budget' => '2500']],
]);

$raised = $lead->with(['budget' => '4000']);

var_dump($lead->budget, $raised->budget);
echo $lead->at->format('l, j M Y'), PHP_EOL;
int(2500)
int(4000)
Monday, 2 Mar 2026

What else it does

  • Cast a value to a backed enum. A value that isn't one of its cases throws, so you notice when an API starts sending a new status.
  • Cast a sub-object to another definition, and a list to a collection of them.
  • Use your own cast classes. They implement CastsAttributes, the same contract you use for an Eloquent model.
  • getOrFail() throws when a value is missing, and the message names the path. A missing key and a null are not the same thing.
  • toArray() and json() return the declared values by their short names, so you can pass a definition to a response.
  • makeMany() wraps every item of a list or a generator.

When to use it

Use this package for payloads you receive and don't control. It doesn't validate or transform data.

If you define the data yourself, or you need validation, use spatie/laravel-data. If you only read a few values, Laravel's Fluent is enough. It reads dot-paths and has typed getters, but it is writable, has no aliases, and its integer() returns 0 for "n/a".

Installation

You can install the package via composer:

composer require keaton-php/laravel-data-definition

The package requires PHP 8.2 or higher and Laravel 11, 12 or 13. It has no service provider, config file or migration.

Documentation

You'll find the documentation on kevineaton.dev/docs/laravel-data-definition. Basic usage is a good place to start. The same pages are in the docs folder of this repository:

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.