ModelResource

Fields

Video guide

Fields in MoonShine are used not only for entering data, but also for outputting it. In most cases, they refer to table fields from the database.

In MoonShine, there are many types of fields that cover all possible requirements! They also encompass all possible relationships in Laravel and are conveniently named after the relationship methods BelongsTo, BelongsToMany, HasOne, HasMany, HasOneThrough, HasManyThrough, MorphOne, MorphMany.

Adding fields to resource pages is easy! To do this, you need to declare the fields in the fields() method on the appropriate pages.

 namespaces
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
 
class PostIndexPage extends IndexPage
{
protected function fields(): iterable
{
return [
ID::make()->sortable(),
Text::make('Title'),
];
}
}
 namespaces
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
 
class PostIndexPage extends IndexPage
{
protected function fields(): iterable
{
return [
ID::make()->sortable(),
Text::make('Title'),
];
}
}
 namespaces
use MoonShine\UI\Components\Layout\Box;
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
 
class PostFormPage extends FormPage
{
protected function fields(): iterable
{
return [
Box::make([
ID::make(),
Text::make('Title')
->required(),
Text::make('Subtitle')
->nullable(),
]),
];
}
}
 namespaces
use MoonShine\UI\Components\Layout\Box;
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
 
class PostFormPage extends FormPage
{
protected function fields(): iterable
{
return [
Box::make([
ID::make(),
Text::make('Title')
->required(),
Text::make('Subtitle')
->nullable(),
]),
];
}
}
 namespaces
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
 
class PostDetailPage extends DetailPage
{
protected function fields(): iterable
{
return [
ID::make(),
Text::make('Title'),
Text::make('Subtitle'),
];
}
}
 namespaces
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
 
class PostDetailPage extends DetailPage
{
protected function fields(): iterable
{
return [
ID::make(),
Text::make('Title'),
Text::make('Subtitle'),
];
}
}