MoonShine Documentation

website·Indexed 33 days ago

https://moonshine.cutcode.dev

Documentation for MoonShine, covering components such as Paginations, Tables, Cards, and Modals, as well as filters like SelectFilter and SwitchFilter, and fields including MorphMany and StackFields. This version is an older release; users are encouraged to upgrade to MoonShine 2.x.

Tokens
111K
Snippets
849
Records
895
Agent score
47%

What's inside MoonShine

  1. Extend MoonShine core functionality

    MoonShine allows developers to extend its core functionality by creating custom packages or extending specific entities. The following entities are available for extension:

    • Fields
    • Filters
    • Decorations
    • Actions
    • Metrics
    • InputExtension
    • FormComponent
    • Resource

    For developers creating a new package, MoonShine provides an official package template: https://github.com/moonshine-software/moonshine-package-template

  2. Extend MoonShine core functionality

    MoonShine allows developers to extend basic functionality or create custom packages. The following entities are available for extension:

    • Fields
    • Filters
    • Decorations
    • Actions
    • Metrics
    • InputExtension
    • FormComponent
    • Resource

    For those creating a new package, a ready-made template is available on GitHub.

    # Package template available at:
    https://github.com/moonshine-software/moonshine-package-template
  3. Extend MoonShine functionality via custom components

    MoonShine allows developers to extend its core functionality by creating custom implementations of specific entities. This is useful for building custom packages or adding specialized behavior to the admin panel. The following entities can be extended:

    • Fields
    • Filters
    • Decorations
    • Actions
    • Metrics
    • InputExtension
    • FormComponent
    • Resource
  4. Manage Laravel hasOne relationships with the HasOne field

    The HasOne field is used for Laravel relationships of type hasOne. It allows creating a new record in the related table and linking it to the current record. If a relationship already exists, the existing record is edited.

    When defining the nested fields within the fields() method of a HasOne component, including the ID field is mandatory.

    If the nested fields are too numerous to be displayed comfortably in the default inline view, use the fullPage() method to render them in a standard full-page layout. Additionally, resourceMode is available for advanced configuration (refer to HasMany documentation for details).

    use MoonShine	extbf{Fields}\{HasOne, ID, Text"};
    
    // Inside your resource class
    public function fields(): array
    {
        return [
            HasOne::make('City', 'city', 'name')
                ->fields([
                    ID::make(), // Required
                    Text::make('Value', 'name'),
                ])
                ->fullPage(), // Optional: renders fields in a full-page view instead of inline
        ];
    }
  5. Add pivot table fields to a BelongsToMany relationship

    To include and edit fields located on the pivot table of a BelongsToMany relationship, use the fields() method on the BelongsToMany field instance.
    use MoonShine
    Fields
    BelongsToMany;
    use MoonShine
    Fields
    Text;
    
    public function fields(): array
    {
        return [
            BelongsToMany::make('Contacts', 'contacts', 'name')
                ->fields([
                    Text::make('Contact', 'text'),
                ])
         ];
    }
  6. Display read-only data using the NoInput field

    The NoInput field is used to display text data from a model or generate custom text that should not be editable by the user. It is strictly for data presentation, not for input or amendment.
    use MoonShine\Fields\NoInput;
    
    public function fields(): array
    {
        return [
            NoInput::make('No input field', 'no_input', static fn() => fake()->realText()),
        ];
    }
  7. Register resources and menu items in MoonShine

    Resources must be registered in the MoonShineServiceProvider using the MoonShine singleton. You can register them as general resources or specifically as menu items to make them visible in the navigation sidebar.
    namespace App\Providers;
    
    use App\MoonShine\Resources\PostResource;
    use Illuminate\Support\ServiceProvider;
    use MoonShine\Menu\MenuItem;
    use MoonShine\MoonShine;
    
    class MoonShineServiceProvider extends ServiceProvider
    {
        public function boot()
        {
            // Register resources in the system
            app(MoonShine::class)->resources([
                new PostResource(),
            ]);
    
            // Register resources in the navigation menu
            app(MoonShine::class)->menu([
                MenuItem::make('Posts', new PostResource()),
            ]);
        }
    }
  8. Add a generated page class to the menu

    Once a page class (e.g., ExamplePage) has been generated via the CLI, instantiate the class and pass it as the second argument to MenuItem::make() to include it in the MoonShine menu.
    use Illuminate\Support\ServiceProvider;
    use MoonShine\Menu\MenuItem;
    use MoonShine\MoonShine;
    use MoonShine\Pages\ExamplePage;
    
    class MoonShineServiceProvider extends ServiceProvider
    {
        public function boot(): void
        {
            app(MoonShine::class)->menu([
                MenuItem::make('Example', new ExamplePage())
            ]);
        }
    }