Fields

StackFields

Basics

Contains all Basic Methods.

The StackFields field allows you to group fields when displaying in the preview.

The fields() method should accept an array of fields to group.

 namespaces
use MoonShine\Laravel\Fields\BelongsTo;
use MoonShine\UI\Fields\StackFields;
use MoonShine\UI\Fields\Text;
 
StackFields::make('Title')->fields([
Text::make('Title'),
BelongsTo::make('Author', resource: 'name'),
])
 namespaces
use MoonShine\Laravel\Fields\BelongsTo;
use MoonShine\UI\Fields\StackFields;
use MoonShine\UI\Fields\Text;
 
StackFields::make('Title')->fields([
Text::make('Title'),
BelongsTo::make('Author', resource: 'name'),
])

Edit View

You can customize the display for StackFields using components.

StackFields::make('Title')->fields([
Text::make('Title'),
LineBreak::make(), // adds a line break
BelongsTo::make('Author', resource: 'name'),
])
StackFields::make('Title')->fields([
Text::make('Title'),
LineBreak::make(), // adds a line break
BelongsTo::make('Author', resource: 'name'),
])

Conditional Display

To change the set of components in StackFields under certain conditions, you need to pass a condition and sets of components using a callback function.

StackFields::make('Stack')
->fields(
fn(StackFields $ctx) => $ctx->getData()?->getOriginal()->id === 3 ? [
Date::make('Creation date', 'created_at'),
] : [
Date::make('Creation date', 'created_at'),
LineBreak::make(),
Email::make('Email', 'email'),
]
)
StackFields::make('Stack')
->fields(
fn(StackFields $ctx) => $ctx->getData()?->getOriginal()->id === 3 ? [
Date::make('Creation date', 'created_at'),
] : [
Date::make('Creation date', 'created_at'),
LineBreak::make(),
Email::make('Email', 'email'),
]
)