Evolving the API

Renaming and retyping

Declare what a field was in older revisions with was(): its name, its type, or both.

was(R, name:, schema:, down:, up:) declares that up to R included, the field had another name, another type, or both. The head key stays the source: the field is always read under its head key, then renamed and converted.

Rename a field

'status' => Schema::enum(OrderStatus::class)
    ->was(ApiRevision::Winter25, name: 'state'),
ClientResponse keyRequest keyController reads
spring-26statusstatusstatus
winter-25, genesisstatestatestatus

Query parameters and nested fields are renamed the same way:

'notify' => Schema::bool()->was(ApiRevision::Winter25, name: 'send_email'),
'lines' => Schema::list(Schema::object([
    'quantity' => Schema::int()->was(ApiRevision::Genesis, name: 'qty'),
])),

Change a type

schema: gives the older type. Most scalar changes convert automatically; give down: (responses) and up: (requests) when the automatic cast is not what your clients need, or does not exist. See Conversions.

'status' => Schema::enum(OrderStatus::class)
    ->was(
        ApiRevision::Genesis,
        name: 'paid',
        schema: Schema::bool(),
        down: fn(OrderStatus $status): bool
            => $status === OrderStatus::Paid,
        up: fn(bool $paid): OrderStatus => $paid
            ? OrderStatus::Paid
            : OrderStatus::Pending,
    ),

down: receives the PHP value of the newer shape, normalized: an OrderStatus case, not 'paid'. It returns the PHP value of the older shape; serialization comes after. up: mirrors it.

Chained deltas

A field may have several was(). Each describes the shape just before the next newer one, and an argument left out keeps the value of that newer shape. They are ordered by revision whatever the call order; writing them newest first reads like the history.

'total' => Schema::int('total_cents')
    ->was(
        ApiRevision::Winter25,
        schema: Schema::string(),
        down: fn(int $cents): string => number_format($cents / 100, 2, '.', ''),
    )
    ->was(
        ApiRevision::Genesis,
        schema: Schema::float(),
        down: fn(string $amount): float => (float) $amount,
    ),
Clienttotal
spring-261250
winter-25"12.50"
genesis12.5

The genesis closure receives what the winter-25 one returned, a string, not the int of the head.

A was() has no source

An older value is always derived from the head value. When it needs more, read the subject given as the second argument of down:, or declare the old field with until() and its own source next to the new one.