MoonShine provides the ability to configure CRUD pages.
To do this, you need to choose the resource type Model resource with pages when creating a resource via the command.
This will create a model resource class and additional classes for the index, detail view, and form pages.
The page classes will, by default, be located in the app/MoonShine/Pages directory.
In the created model resource, CRUD pages will be registered in the pages() method.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:start]
namespace App\MoonShine\Resources;
use App\MoonShine\Pages\Post\PostIndexPage;
use App\MoonShine\Pages\Post\PostFormPage;
use App\MoonShine\Pages\Post\PostDetailPage;
use MoonShine\Laravel\Resources\ModelResource; // [tl! collapse:end]
class PostResource extends ModelResource
{
// ...
protected function pages(): array
{
return [
PostIndexPage::class,
PostFormPage::class,
PostDetailPage::class,
];
}
}
namespaces
namespaceApp\MoonShine\Resources;
useApp\MoonShine\Pages\Post\PostIndexPage;
useApp\MoonShine\Pages\Post\PostFormPage;
useApp\MoonShine\Pages\Post\PostDetailPage;
useMoonShine\Laravel\Resources\ModelResource;
classPostResourceextendsModelResource
{
// ...
protectedfunctionpages():array
{
return [
PostIndexPage::class,
PostFormPage::class,
PostDetailPage::class,
];
}
}
namespace App\MoonShine\Resources;
use App\MoonShine\Pages\Post\PostIndexPage;
use App\MoonShine\Pages\Post\PostFormPage;
use App\MoonShine\Pages\Post\PostDetailPage;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function pages(): array
{
return [
PostIndexPage::class,
PostFormPage::class,
PostDetailPage::class,
];
}
}
Fields in MoonShine are used not only for data input but also for output.
The fields() method in the CRUD page class allows you to specify the necessary fields.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:5]
namespace App\MoonShine\Pages\Post;
use MoonShine\Laravel\Pages\Crud\IndexPage;
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
class PostIndexPage extends IndexPage
{
// ...
protected function fields(): iterable
{
return [
ID::make(),
Text::make('Title'),
];
}
}
namespaces
namespaceApp\MoonShine\Pages\Post;
useMoonShine\Laravel\Pages\Crud\IndexPage;
useMoonShine\UI\Fields\ID;
useMoonShine\UI\Fields\Text;
classPostIndexPageextendsIndexPage
{
// ...
protectedfunctionfields():iterable
{
return [
ID::make(),
Text::make('Title'),
];
}
}
namespace App\MoonShine\Pages\Post;
use MoonShine\Laravel\Pages\Crud\IndexPage;
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
class PostIndexPage extends IndexPage
{
// ...
protected function fields(): iterable
{
return [
ID::make(),
Text::make('Title'),
];
}
}
For convenience, all CRUD pages are divided into three layers, which are responsible for displaying a certain area on the page.
TopLayer - used for displaying metrics on the index page and for additional buttons on the edit page,
MainLayer - this layer is used for displaying main information using FormBuilder and TableBuilder,
BottomLayer - used for displaying additional information.
To configure the layers, the corresponding methods are used: topLayer(), mainLayer(), and bottomLayer().
The methods must return an array of Components.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:2]
use MoonShine\Laravel\Pages\Crud\IndexPage;
use MoonShine\UI\Components\Heading;
class PostIndexPage extends IndexPage
{
// ...
protected function topLayer(): array
{
return [
Heading::make('Custom top'),
...parent::topLayer()
];
}
protected function mainLayer(): array
{
return [
Heading::make('Custom main'),
...parent::mainLayer()
];
}
protected function bottomLayer(): array
{
return [
Heading::make('Custom bottom'),
...parent::bottomLayer()
];
}
}
namespaces
useMoonShine\Laravel\Pages\Crud\IndexPage;
useMoonShine\UI\Components\Heading;
classPostIndexPageextendsIndexPage
{
// ...
protectedfunctiontopLayer():array
{
return [
Heading::make('Custom top'),
...parent::topLayer()
];
}
protectedfunctionmainLayer():array
{
return [
Heading::make('Custom main'),
...parent::mainLayer()
];
}
protectedfunctionbottomLayer():array
{
return [
Heading::make('Custom bottom'),
...parent::bottomLayer()
];
}
}
use MoonShine\Laravel\Pages\Crud\IndexPage;
use MoonShine\UI\Components\Heading;
class PostIndexPage extends IndexPage
{
// ...
protected function topLayer(): array
{
return [
Heading::make('Custom top'),
...parent::topLayer()
];
}
protected function mainLayer(): array
{
return [
Heading::make('Custom main'),
...parent::mainLayer()
];
}
protected function bottomLayer(): array
{
return [
Heading::make('Custom bottom'),
...parent::bottomLayer()
];
}
}
If you need to access components of a specific layer from a resource or page, use the getLayerComponents method.
use MoonShine\Support\Enums\Layer;
// ...
// Resource
$this->getFormPage()->getLayerComponents(Layer::BOTTOM);
// Page
$this->getLayerComponents(Layer::BOTTOM);
If you need to add a component to a specified page in the desired layer from a resource, use the resource's onLoad() method and the page's pushToLayer().
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:2]
use MoonShine\Permissions\Components\Permissions;
use MoonShine\Support\Enums\Layer;
protected function onLoad(): void
{
$this->getFormPage()->pushToLayer(
layer: Layer::BOTTOM,
component: Permissions::make(
'Permissions',
$this,
)
);
}
namespaces
useMoonShine\Permissions\Components\Permissions;
useMoonShine\Support\Enums\Layer;
protectedfunctiononLoad():void
{
$this->getFormPage()->pushToLayer(
layer: Layer::BOTTOM,
component: Permissions::make(
'Permissions',
$this,
)
);
}
use MoonShine\Permissions\Components\Permissions;
use MoonShine\Support\Enums\Layer;
protected function onLoad(): void
{
$this->getFormPage()->pushToLayer(
layer: Layer::BOTTOM,
component: Permissions::make(
'Permissions',
$this,
)
);
}