Repeater#

Repeater holds rows edited inside the record’s form, repeater in the descriptor: a JSON list of objects of declared fields, or an owned has-many relation’s child records. It edits them in one of two modes.

Repeater::make('specs', 'Specifications')->fields([
    Text::make('label')->required()->max(60),
    Text::make('value')->required()->max(200),
])->max(20),
A Repeater field on the product form: Specifications, two rows of Label and Value inputs with drag handles and remove buttons, 2 of at most 20, and Add row
A Repeater field on the product form: Specifications, two rows of Label and Value inputs with drag handles and remove buttons, 2 of at most 20, and Add row

JSON mode#

fields([...]) declares the fields of a row, and the rows are stored as a JSON list of objects in one column, an array or ?array property, as in the example above:

{"specs": [{"label": "Warranty", "value": "24 months"}, {"label": "Made in", "value": "Portugal"}]}

A row field is a plain value: text, textarea, markdown, email, URL, slug, color, number, money, boolean, select, currency, date, date-time or tags; never a relation, a file, rich text or another repeater. Each row is converted and checked by its fields’ own rules, a field the row leaves out takes its default (or is required when it may not be empty), and a key no field declares is refused. A row is stored with its fields in the order they are declared; a select keeps its option’s value, a date its YYYY-MM-DD, a date-time its UTC instant as text. The list keeps the order it is sent in, so its rows are always reordered by dragging and orderBy() is refused.

Relationship mode#

relationship('lines') names an owned #[HasMany] of the entity, and each row is one record of the child resource:

Repeater::make('lines')->relationship('lines')
    ->fields(['product', 'quantity', 'unitPriceCents'])
    ->min(1)->max(50)
    ->orderBy('position'),

fields() then names the child fields a row edits (every child field it can write by default, never the one pointing back at the parent). The child resource is the one administering the child entity, or the one marked relationTarget; relationship('lines', OrderLineResource::class) names it. A row with an id updates that child, one without creates it pointing at the parent, and a child left out is deleted. A child the child resource’s scope() hides is not a row of the form: a save leaves it as it is, position included, and a row naming its id is refused as unknown_row, as one naming another record’s child is. Each row is validated, authorized, hooked and audited as the child’s own create, update or delete; Forms has the details.

Ordering#

orderBy('position') names an int property of the child entity: each row’s index (from 0) is written into it on save, and the rows load ordered by it, so the form reorders them by dragging (or with the arrow keys on a row’s handle). The property is not one of the row’s fields; the compiler refuses one that is not an int property of the child, and naming it in fields(). Without orderBy() a relationship repeater’s rows load oldest first and are not dragged.

Bounds, errors and the transaction#

min() and max() bound the number of rows in both modes (0 and 100 by default); a list outside them is refused with count_out_of_range. Errors name their row: specs.2.label, lines.1.quantity, or the row itself (specs.2) when it is not an object. In relationship mode, the record and every row are flushed in one transaction, so one failing row (a database constraint included) writes nothing. A unique key a child row collides with is reported at that row, and two rows sending the same value for a child field marked unique() are refused at the later row before anything is written.

The child resource must live on the same connection as the parent, since its save is one transaction; the ORM already refuses a relationship across two connections. The compiler refuses a relation that is not an owned has-many, a soft-deleting child, a child field a new row requires left out of fields(), a row holding another repeater, and conditions on the repeater. A repeater is never sorted, grouped, filtered, searched, summarized or edited inline. A list shows how many rows a record holds; the record page shows them as a table. A JSON repeater is exported and imported as JSON text; a relationship repeater is not imported, edited in bulk or reverted from the audit.

A JSON repeater’s rows nest only so deep; see How deep a value nests.