# Repeater (array)

{% tabs %}
{% tab title="Preview" %}
![Default mode is to display labels above the first iteration](https://3246461946-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQWt7hBSh108moAc0Al%2Fuploads%2F1XKmXakcolRciTvPOglO%2FRepeaterLabelsOnTop.png?alt=media\&token=12152629-d7c6-4e5d-b26d-06a2e3557e55)

![Or you can repeat the labels on each item](https://3246461946-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQWt7hBSh108moAc0Al%2Fuploads%2F7zxyLEPFMGZO00DmoekZ%2FRepeaterLabelEachRow.png?alt=media\&token=3f087aff-c6c8-4a08-8e2f-cc51dcca7150)
{% endtab %}

{% tab title="Component" %}
```php
Repeater::make('Friends')->fields([
    Input::make('First name')
        ->afterLabel('After Label slot')
        ->rules('required'),
    Input::make('Last name')
        ->afterLabel('Best slot to display field help')
        ->rules('required'),
])
    ->sortable()
    ->rules('required')
    ->help('Click plus to add a friend')
    ->confirmDelete()
    ->default([[]]) // show empty item by default
    ->childColspan(6)
    //->labelEachRow() repeat labels above each iteration
```



### Show an empty item by default

Set an empty array item as `->default()` to be displayed if the field value is empty.

```php
Repeater::make('Your repeater')
    ->fields([ your fields here ])
    ->default([[]])
```

Use `updatedFoo()` hook to add an empty array if the users deletes the empty item

```php
public function updatedYourRepeater($value) {
    if(blank($value)) data_set($this, 'form_data.your_repeater', [[]]);
}
```
{% endtab %}

{% tab title="Headless" %}
Not supported. Check the source in \
`resources/views/includes/array-wrapper.blade.php` \
if you want to replicate the layout, manually.
{% endtab %}

{% tab title="Icons" %}
You can change the default blade views for the icons in the config file.

```php
//icons
'arrow-up-icon' => 'icons.cheveron-outline-up',
'arrow-down-icon' => 'icons.cheveron-outline-down',
'trash-icon' => 'icons.close-outline',
```
{% endtab %}
{% endtabs %}



## Methods

### ->fields($fields = \[ ])

You can use almost any field type, an error will be thrown if you use an invalid field.

### ->labelEachRow()

* Show all field slots (labels and more) on every iteration.
* Without this, the label will only be displayed above the first row.

### ->sortable()

Makes the array sortable. Show buttons to change the order of the array items. (See the image)

### ->wrapperClass(string $classes)

* Applied to the outer wrapper surrounding the Repeater field group
* Default: `'flex flex-col divide-y mb-2 rounded border'`

### ->wrapperGrid(string $classes)

* Defines the css grid for the Repeater field group
* Default: `'flex-1 sm:grid sm:grid-cols-12 gap-x-2'`

### ->childColspan(int $cols)

* Overrides nested `$field->colspan()`
* Default 12 of 12 columns
* If you set it on the `Panel` you do not have to set it on each nested field.

### ->childStacked()

* Apply `$field->stacked()` to all nested fields, can be overridden in each field.

### ->childInline()

* Apply `$field->inline()` to all nested fields, can be overridden in each field.

### ->wire(string $wire\_model\_declaration)

* Override nested fields `wire:model` attribute.

### ->confirmDelete(string $message = '')

* Show an alert to confirm item deletion.
* Custom message
* Default message = config translation string `'are-u-sure'`
