Components

You're browsing the documentation for an old version of MoonShine. Consider upgrading your project to MoonShine 2.x.

# Basics

You can add your own components based on the MoonShine\IndexComponent, MoonShine\FormComponent or MoonShine\DetailComponent abstract classes to extend the functionality, they will be displayed below the main form or below detailed information respectively

All custom components must be registered in the components() method

namespace MoonShine\Resources;
 
class ArticleResource extends Resource
{
//...
 
public function components(): array
{
return [
// ...
];
}
 
//...
}

# Permissions

Example of adding a PermissionFormComponent component

namespace MoonShine\Resources;
 
use MoonShine\FormComponents\PermissionFormComponent;
use MoonShine\Models\MoonshineUserRole;
use MoonShine\Resources\Resource;
 
class MoonShineUserResource extends Resource
{
//...
 
public function components(): array
{
return [
PermissionFormComponent::make('Permissions')
->canSee(fn() => auth()->user()->moonshine_user_role_id === MoonshineUserRole::DEFAULT_ROLE_ID)
];
}
 
//...
}

If you use your model for authentication, then you need to add the MoonShine\Traits\Models\HasMoonShinePermissions trait to it

If you use your resource with admin output, you need to add the MoonShine\Traits\Resource\WithUserPermissions trait to the resource

# Change log

To display the log of edits in the admin panel based on the user you must add the MoonShine\Traits\Models\HasMoonShineChangeLog trait to the model being used in the resource

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use MoonShine\Traits\Models\HasMoonShineChangeLog;
 
class Article extends Model
{
use HasMoonShineChangeLog;
 
//...
}

And also add a component in the ChangeLogFormComponent resource

namespace MoonShine\Resources;
 
use MoonShine\FormComponents\ChangeLogFormComponent;
use MoonShine\Models\MoonshineUserRole;
use MoonShine\Resources\Resource;
 
class ArticleResource extends Resource
{
// ...
public function components(): array
{
return [
ChangeLogFormComponent::make('Change log')
->canSee(fn() => auth()->user()->moonshine_user_role_id === MoonshineUserRole::DEFAULT_ROLE_ID),
];
}
// ...
}