ModelResource extends CrudResource and provides functionality for working with Eloquent models.
It serves as a foundation for creating resources associated with database models.
ModelResource offers methods for performing CRUD operations, managing relationships, applying filters, and much more.
You can also refer to the section on CrudResource.
CrudResource is an abstract class providing a basic interface for CRUD operations without binding to a storage and data type.
Under the hood, ModelResource extends CrudResource and immediately includes the capability to work with Eloquent.
If you delve into the details of MoonShine, you will see all the standard Controller, Model, and blade views.
If you were developing independently, you could create resource controllers and resource routes as follows:
The resource is automatically registered in MoonShineServiceProvider when executing the command php artisan moonshine:resource.
However, if you create a section manually, you need to declare it in the system within MoonShineServiceProvider.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:start]
namespace App\Providers;
use App\MoonShine\Resources\ArticleResource;
use Illuminate\Support\ServiceProvider;
use MoonShine\Contracts\Core\DependencyInjection\CoreContract;
use MoonShine\Laravel\DependencyInjection\ConfiguratorContract;
use MoonShine\Laravel\DependencyInjection\MoonShine;
use MoonShine\Laravel\DependencyInjection\MoonShineConfigurator; // [tl! collapse:end]
class MoonShineServiceProvider extends ServiceProvider
{
/**
* @param MoonShine $core
* @param MoonShineConfigurator $config
*
*/
public function boot(
CoreContract $core,
ConfiguratorContract $config,
): void
{
$core
->resources([
MoonShineUserResource::class,
MoonShineUserRoleResource::class,
ArticleResource::class,
// ...
])
->pages([
...$config->getPages(),
])
;
}
}
All pages in MoonShine have a Layout, and each page can have its own.
By default, when MoonShine is installed, a base MoonShineLayout is added to the directory app/MoonShine/Layouts.
In Layout, everything related to the appearance of your pages, including navigation, is customized.
To add a section to the menu, you need to declare it via the menu() method in Layout.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:start]
namespace App\MoonShine\Layouts;
use App\MoonShine\Resources\PostResource;
use MoonShine\Laravel\Layouts\CompactLayout;
use MoonShine\Laravel\Resources\MoonShineUserResource;
use MoonShine\Laravel\Resources\MoonShineUserRoleResource;
use MoonShine\MenuManager\MenuGroup;
use MoonShine\MenuManager\MenuItem; // [tl! collapse:end]
final class MoonShineLayout extends CompactLayout
{
// ...
protected function menu(): array
{
return [
MenuGroup::make(__('moonshine::ui.resource.system'), [
MenuItem::make(
__('moonshine::ui.resource.admins_title'),
MoonShineUserResource::class
),
MenuItem::make(
__('moonshine::ui.resource.role_title'),
MoonShineUserRoleResource::class
),
]),
MenuItem::make('Posts', PostResource::class),
// ...
];
}
}
By default, the alias of the resource used in the url is generated based on the class name in kebab-case, for example:
MoonShineUserResource -> moon-shine-user-resource.
To change the alias, you can use the resource property $alias or the method getAlias().
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
protected ?string $alias = 'custom-alias';
// ...
}
or
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:3]
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
public function getAlias(): ?string
{
return 'custom-alias';
}
}
namespaces
namespaceApp\MoonShine\Resources;
useMoonShine\Laravel\Resources\ModelResource;
classPostResourceextendsModelResource
{
publicfunctiongetAlias():?string
{
return'custom-alias';
}
}
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
public function getAlias(): ?string
{
return 'custom-alias';
}
}
If the resourceItem parameter is present in the url of the detail or editing page, you can access the current element in the resource using the getItem() method.
$this->getItem();
$this->getItem();
$this->getItem();
You can access the model through the getModel() method.
By default, when creating and editing a record, a redirect to the form page is performed, but this behavior can be controlled.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:1]
use MoonShine\Support\Enums\PageType;
// Through a property in the resource
protected ?PageType $redirectAfterSave = PageType::FORM;
// or through methods (redirect after deletion is also available)
public function getRedirectAfterSave(): string
{
return '/';
}
public function getRedirectAfterDelete(): string
{
return $this->getIndexPageUrl();
}
// or through methods (redirect after deletion is also available)
publicfunctiongetRedirectAfterSave():string
{
return'/';
}
publicfunctiongetRedirectAfterDelete():string
{
return$this->getIndexPageUrl();
}
use MoonShine\Support\Enums\PageType;
// Through a property in the resource
protected ?PageType $redirectAfterSave = PageType::FORM;
// or through methods (redirect after deletion is also available)
public function getRedirectAfterSave(): string
{
return '/';
}
public function getRedirectAfterDelete(): string
{
return $this->getIndexPageUrl();
}
Often, it is necessary to create a resource where the ability to delete, add, or edit is excluded.
This is not about authorization, but rather a global exclusion of these sections.
This can be done easily through the activeActions() method in the resource.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:5]
namespace App\MoonShine\Resources;
use MoonShine\Support\ListOf;
use MoonShine\Laravel\Enums\Action;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function activeActions(): ListOf
{
return parent::activeActions()
->except(Action::VIEW, Action::MASS_DELETE)
// ->only(Action::VIEW)
;
}
}
namespaces
namespaceApp\MoonShine\Resources;
useMoonShine\Support\ListOf;
useMoonShine\Laravel\Enums\Action;
useMoonShine\Laravel\Resources\ModelResource;
classPostResourceextendsModelResource
{
// ...
protectedfunctionactiveActions():ListOf
{
returnparent::activeActions()
->except(Action::VIEW, Action::MASS_DELETE)
// ->only(Action::VIEW)
;
}
}
namespace App\MoonShine\Resources;
use MoonShine\Support\ListOf;
use MoonShine\Laravel\Enums\Action;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function activeActions(): ListOf
{
return parent::activeActions()
->except(Action::VIEW, Action::MASS_DELETE)
// ->only(Action::VIEW)
;
}
}
You can also create a new list, for example:
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:2]
use MoonShine\Laravel\Enums\Action;
use MoonShine\Support\ListOf;
protected function activeActions(): ListOf
{
return new ListOf(Action::class, [Action::VIEW, Action::UPDATE]);
}
use MoonShine\Laravel\Enums\Action;
use MoonShine\Support\ListOf;
protected function activeActions(): ListOf
{
return new ListOf(Action::class, [Action::VIEW, Action::UPDATE]);
}
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\Support\AlpineJs;
use MoonShine\Support\Enums\JsEvent;
use MoonShine\Support\ListOf;
use MoonShine\UI\Components\ActionButton;
class PostResource extends ModelResource
{
// ...
protected function topButtons(): ListOf
{
return parent::topButtons()->add(
ActionButton::make('Refresh', '#')
->dispatchEvent(AlpineJs::event(JsEvent::TABLE_UPDATED, $this->getListComponentName()))
);
}
}
To modify the main component of IndexPage, FormPage, or DetailPage from the resource, you can override the corresponding methods modifyListComponent(), modifyFormComponent(), and modifyDetailComponent().
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:1]
use MoonShine\Contracts\UI\ComponentContract;
public function modifyListComponent(ComponentContract $component): ComponentContract
{
return parent::modifyListComponent($component)->customAttributes([
'data-my-attr' => 'value'
]);
}
use MoonShine\Contracts\UI\ComponentContract;
public function modifyListComponent(ComponentContract $component): ComponentContract
{
return parent::modifyListComponent($component)->customAttributes([
'data-my-attr' => 'value'
]);
}
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:2]
use MoonShine\Contracts\UI\ComponentContract;
use MoonShine\UI\Components\FlexibleRender;
public function modifyFormComponent(ComponentContract $component): ComponentContract
{
return parent::modifyFormComponent($component)->fields([
FlexibleRender::make('Top'),
...parent::modifyFormComponent($component)->getFields()->toArray(),
FlexibleRender::make('Bottom'),
])->submit('Go');
}
use MoonShine\Contracts\UI\ComponentContract;
use MoonShine\UI\Components\FlexibleRender;
public function modifyFormComponent(ComponentContract $component): ComponentContract
{
return parent::modifyFormComponent($component)->fields([
FlexibleRender::make('Top'),
...parent::modifyFormComponent($component)->getFields()->toArray(),
FlexibleRender::make('Bottom'),
])->submit('Go');
}
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:1]
use MoonShine\Contracts\UI\ComponentContract;
public function modifyDetailComponent(ComponentContract $component): ComponentContract
{
return parent::modifyDetailComponent($component)->customAttributes([
'data-my-attr' => 'value'
]);
}
use MoonShine\Contracts\UI\ComponentContract;
public function modifyDetailComponent(ComponentContract $component): ComponentContract
{
return parent::modifyDetailComponent($component)->customAttributes([
'data-my-attr' => 'value'
]);
}
The best way to change page components is to publish the pages and interact through them.
But, if you want to quickly add components to pages, you can use the resource methods pageComponents(), indexPageComponents(), formPageComponents() and detailPageComponents().
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:4]
use MoonShine\Core\Collections\Components;
use MoonShine\UI\Components\FormBuilder;
use MoonShine\UI\Components\Modal;
use MoonShine\UI\Fields\Text;
// or indexPageComponents/formPageComponents/detailPageComponents
protected function pageComponents(): array
{
return [
Modal::make(
'My Modal'
components: Components::make([
FormBuilder::make()->fields([
Text::make('Title')
])
])
)
->name('demo-modal')
];
}
namespaces
useMoonShine\Core\Collections\Components;
useMoonShine\UI\Components\FormBuilder;
useMoonShine\UI\Components\Modal;
useMoonShine\UI\Fields\Text;
// or indexPageComponents/formPageComponents/detailPageComponents
protectedfunctionpageComponents():array
{
return [
Modal::make(
'My Modal'
components: Components::make([
FormBuilder::make()->fields([
Text::make('Title')
])
])
)
->name('demo-modal')
];
}
use MoonShine\Core\Collections\Components;
use MoonShine\UI\Components\FormBuilder;
use MoonShine\UI\Components\Modal;
use MoonShine\UI\Fields\Text;
// or indexPageComponents/formPageComponents/detailPageComponents
protected function pageComponents(): array
{
return [
Modal::make(
'My Modal'
components: Components::make([
FormBuilder::make()->fields([
Text::make('Title')
])
])
)
->name('demo-modal')
];
}
You can also attach a trait to the resource and within the trait, add a method according to the naming convention - load{TraitName} and use the trait to access the onLoad of the resource.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:4]
namespace App\MoonShine\Resources;
use App\Traits\WithPermissions;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
use WithPermissions;
// ...
}
namespaces
namespaceApp\MoonShine\Resources;
useApp\Traits\WithPermissions;
useMoonShine\Laravel\Resources\ModelResource;
classPostResourceextendsModelResource
{
useWithPermissions;
// ...
}
namespace App\MoonShine\Resources;
use App\Traits\WithPermissions;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
use WithPermissions;
// ...
}
The onBoot method allows integration at the moment when MoonShine is creating an instance of the resource within the system.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:3]
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function onBoot(): void
{
// ...
}
}
namespaces
namespaceApp\MoonShine\Resources;
useMoonShine\Laravel\Resources\ModelResource;
classPostResourceextendsModelResource
{
// ...
protectedfunctiononBoot():void
{
// ...
}
}
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function onBoot(): void
{
// ...
}
}
You can also attach a trait to the resource and within the trait, add a method according to the naming convention - boot{TraitName} and use the trait to access the onBoot() of the resource.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:2]
use MoonShine\AssetManager\Css;
use MoonShine\AssetManager\Js;
protected function onLoad(): void
{
$this->getAssetManager()
->add(Css::make('/css/app.css'))
->append(Js::make('/js/app.js'));
}
namespaces
useMoonShine\AssetManager\Css;
useMoonShine\AssetManager\Js;
protectedfunctiononLoad():void
{
$this->getAssetManager()
->add(Css::make('/css/app.css'))
->append(Js::make('/js/app.js'));
}
use MoonShine\AssetManager\Css;
use MoonShine\AssetManager\Js;
protected function onLoad(): void
{
$this->getAssetManager()
->add(Css::make('/css/app.css'))
->append(Js::make('/js/app.js'));
}
If the resource is in async mode, then you can modify the answer:
public function modifyDestroyResponse(MoonShineJsonResponse $response): MoonShineJsonResponse
{
return $response;
}
public function modifyMassDeleteResponse(MoonShineJsonResponse $response): MoonShineJsonResponse
{
return $response;
}
public function modifySaveResponse(MoonShineJsonResponse $response): MoonShineJsonResponse
{
return $response;
}
public function modifyDestroyResponse(MoonShineJsonResponse $response): MoonShineJsonResponse
{
return $response;
}
public function modifyMassDeleteResponse(MoonShineJsonResponse $response): MoonShineJsonResponse
{
return $response;
}
public function modifySaveResponse(MoonShineJsonResponse $response): MoonShineJsonResponse
{
return $response;
}