Fields

RelationRepeater

Basics

Contains all Basic Methods.

The RelationRepeater field is designed for working with HasMany and HasOne relationships. It allows you to create, edit, and delete related records directly from the main model's form.

The field automatically syncs related records when saving the main model.

make(
string|Closure $label,
?string $relationName = null,
string|Closure|null $formatted = null,
ModelResource|string|null $resource = null,
)
make(
string|Closure $label,
?string $relationName = null,
string|Closure|null $formatted = null,
ModelResource|string|null $resource = null,
)
  • $label - field title,
  • $relationName - relationship name,
  • $formatted - closure for formatting the field's value in "preview" mode,
  • $resource - resource of the related model.
 namespaces
use MoonShine\Laravel\Fields\Relationships\RelationRepeater;
 
RelationRepeater::make(
'Comments',
'comments',
resource: CommentResource::class
)
 namespaces
use MoonShine\Laravel\Fields\Relationships\RelationRepeater;
 
RelationRepeater::make(
'Comments',
'comments',
resource: CommentResource::class
)

Field Set

By default, the field uses all form fields from the specified resource. However, you can override the field set using the fields() method.

 namespaces
use MoonShine\Laravel\Fields\Relationships\RelationRepeater;
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
 
RelationRepeater::make('Characteristics', 'characteristics')
->fields([
ID::make(),
Text::make('Name', 'name'),
Text::make('Value', 'value'),
])
 namespaces
use MoonShine\Laravel\Fields\Relationships\RelationRepeater;
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
 
RelationRepeater::make('Characteristics', 'characteristics')
->fields([
ID::make(),
Text::make('Name', 'name'),
Text::make('Value', 'value'),
])

The presence of the ID field is required, otherwise records will always be added.

Vertical Mode

The vertical() method changes the display of the table from horizontal mode to vertical.

vertical(Closure|bool|null $condition = null)
vertical(Closure|bool|null $condition = null)
RelationRepeater::make('Comments', 'comments')
->vertical()
RelationRepeater::make('Comments', 'comments')
->vertical()

Add/Delete

By default, the field allows adding new items. This behavior can be changed using the creatable() method.

creatable(
Closure|bool|null $condition = null,
?int $limit = null,
?ActionButtonContract $button = null
)
creatable(
Closure|bool|null $condition = null,
?int $limit = null,
?ActionButtonContract $button = null
)
  • $condition - the condition under which the method should be applied,
  • $limit - limit on the number of possible elements,
  • $button - ability to replace the adding button with your own.

The ability to delete items is handled by the removable() method.

removable(
Closure|bool|null $condition = null,
array $attributes = []
)
removable(
Closure|bool|null $condition = null,
array $attributes = []
)
RelationRepeater::make('Comments', 'comments')
->creatable(limit: 5)
->removable()
RelationRepeater::make('Comments', 'comments')
->creatable(limit: 5)
->removable()

Buttons

The buttons() method allows overriding the buttons used in the field.

 namespaces
use MoonShine\Laravel\Fields\Relationships\RelationRepeater;
use MoonShine\UI\Components\ActionButton;
 
RelationRepeater::make('Comments', 'comments')
->buttons([
ActionButton::make('', '#')
->icon('trash')
->onClick(fn() => 'remove()', 'prevent')
->class('btn-error')
->showInLine()
])
 namespaces
use MoonShine\Laravel\Fields\Relationships\RelationRepeater;
use MoonShine\UI\Components\ActionButton;
 
RelationRepeater::make('Comments', 'comments')
->buttons([
ActionButton::make('', '#')
->icon('trash')
->onClick(fn() => 'remove()', 'prevent')
->class('btn-error')
->showInLine()
])

Modifiers

Table Modifier

The modifyTable() method allows modifying the table (TableBuilder).

 namespaces
use MoonShine\Laravel\Fields\Relationships\RelationRepeater;
use MoonShine\UI\Components\Table\TableBuilder;
 
RelationRepeater::make('Comments', 'comments')
->modifyTable(
fn(TableBuilder $table, bool $preview) => $table
->customAttributes([
'class' => 'custom-table'
])
)
 namespaces
use MoonShine\Laravel\Fields\Relationships\RelationRepeater;
use MoonShine\UI\Components\Table\TableBuilder;
 
RelationRepeater::make('Comments', 'comments')
->modifyTable(
fn(TableBuilder $table, bool $preview) => $table
->customAttributes([
'class' => 'custom-table'
])
)

Remove Button Modifier

The modifyRemoveButton() method allows changing the remove button.

 namespaces
use MoonShine\Laravel\Fields\Relationships\RelationRepeater;
use MoonShine\UI\Components\ActionButton;
 
RelationRepeater::make('Comments', 'comments')
->modifyRemoveButton(
fn(ActionButton $button) => $button
->customAttributes([
'class' => 'btn-secondary'
])
)
 namespaces
use MoonShine\Laravel\Fields\Relationships\RelationRepeater;
use MoonShine\UI\Components\ActionButton;
 
RelationRepeater::make('Comments', 'comments')
->modifyRemoveButton(
fn(ActionButton $button) => $button
->customAttributes([
'class' => 'btn-secondary'
])
)