Basics
You're browsing the documentation for an old version of MoonShine. Consider upgrading your project to MoonShine 2.x.
-
Sections
# Basics
What is an administrative panel? Basically, these are partitions based on data from the database, based on eloquent models.
MoonShine is based on standard Laravel resource controllers and resource routes
php artisan make:controller Controller --resource
Route::resources('resources', Controller::class);
Therefore, the MoonShine resources (admin panel sections) are based on the eloquent model.
# Creating a section in the admin panel
php artisan moonshine:resource Post
This will create a Resource class that will be the basis of the new section in the panel.
By default, it is located in the app/MoonShine/Resources/PostResource
directory.
And it will be automatically bound to the app/Models/Post
model.
The section title will keep the Posts name
You can change the model binding and section title along with the command
php artisan moonshine:resource Post --model=CustomPost --title="Articles"
php artisan moonshine:resource Post --model="App\Models\CustomPost" --title="Articles"
# Main properties of the section
The main parameters of the resource that could be changed in order to customize its work
namespace MoonShine\Resources; use MoonShine\Models\MoonshineUser; class PostResource extends Resource{ public static string $model = App\Models\Post::class; // Model public static string $title = 'Articles'; // Section name public static string $subTitle = 'Article Management'; // Text under section heading public static array $with = ['category']; // Eager load public static bool $withPolicy = false; // Authorization public static string $orderField = 'id'; // Default sort field public static string $orderType = 'DESC'; // Default sort type public static int $itemsPerPage = 25; // Number of items per page //...}
# Declaring a partition in the system
New resources are added to the system in the service provider
using the singleton class
MoonShine\MoonShine
.
namespace App\Providers; use App\MoonShine\Resources\PostResource; class MoonShineServiceProvider extends ServiceProvider{ //... public function boot() { app(MoonShine::class)->resources([ new PostResource(), ]) }
To add a resource link to the navigation menu, the resource can be registered using the menu()
method.
namespace App\Providers; use App\MoonShine\Resources\PostResource; use Illuminate\Support\ServiceProvider;use MoonShine\Menu\MenuItem; use MoonShine\MoonShine; use MoonShine\Resources\MoonShineUserResource;use MoonShine\Resources\MoonShineUserRoleResource; class MoonShineServiceProvider extends ServiceProvider{ //... public function boot() { app(MoonShine::class)->menu([ MenuItem::make('Admins', new MoonShineUserResource()), MenuItem::make('Roles', new MoonShineUserRoleResource()), MenuItem::make('Posts', new PostResource()), ]); }}
For advanced settings, see
Digging Deeper > Menu
.
# Current item/model
In the resource you have access to the current element and model via the relevant methods.
$this->getItem();
$this->getModel();
If the element does not yet exist (action create), then the getItem()
method will return NULL
.
# Modal windows
Ability to add, edit and view entries directly on the list page in a modal window.
namespace App\MoonShine\Resources; use App\Models\Post; use MoonShine\Resources\Resource; class PostResource extends Resource{ public static string $model = Post::class; public static string $title = 'Posts'; protected bool $createInModal = true; protected bool $editInModal = true; protected bool $showInModal = true; // ...
# Route after save
After saving the resource, you can specify which route to go to: the list page, the detail page, or the edit page.
Default index
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Resources\Resource; class PostResource extends Resource{ public static string $model = Post::class; protected string $routeAfterSave = 'index'; // index, show, edit // ...}
# Simple pagination
If you don't plan to display the total number of pages, use Simple Pagination
.
This will avoid additional queries on the total number of records in the database.
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Resources\Resource; class PostResource extends Resource{ public static string $model = Post::class; public static bool $simplePaginate = true; // ...}
# Disable pagination
If you do not plan to use pagination, then you can turn it off.
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Resources\Resource; class PostResource extends Resource{ public static string $model = Post::class; protected bool $usePagination = false; // ...}
# Customization of views
You can customize the display of the list and form through
the properties itemsView
, formView
and detailView
.
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Resources\Resource; class PostResource extends Resource{ public static string $model = Post::class; protected string $itemsView = 'moonshine::crud.shared.table'; protected string $formView = 'moonshine::crud.shared.form'; protected string $detailView = 'moonshine::crud.shared.detail-card'; // ...}
Or by overriding the appropriate methods
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Resources\Resource; class PostResource extends Resource{ public static string $model = Post::class; public function itemsView(): string { return $this->itemsView; } public function formView(): string { return $this->formView; } public function detailView(): string { return $this->detailView; } // ...}
# Precognition
Precognition in Laravel allows you to create a "live" check for your application without having to duplicate validation rules.
In Moonshine, you can use precognition when sending requests to your resource.
To do this, set the precognition
property to true
.
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Resources\Resource; class PostResource extends Resource{ public static string $model = Post::class; protected bool $precognition = true; // ...}