How to use relationships in MoonShine?
Eloquent relationships in MoonShine are implemented through the corresponding fields of the same name.
MoonShine supports all possible relationships: BelongsTo
, BelongsToMany
, HasOne
, HasMany
and others.
Consider using relationship fields using the example of BelongsTo
. For example, you have the Post
and Author
models, where each post belongs to one author.
use Illuminate\Database\Eloquent\Relations\BelongsTo; // In Post modelpublic function author(): BelongsTo{ return $this->belongsTo(Author::class);}
use MoonShine\Laravel\Fields\Relationships\BelongsTo; // In MoonShine PostResourcepublic function formFields(): array{ return [ // ... BelongsTo::make('Author', 'author', AuthorResource::class), ];}
For more information about each type of connection, see the relevant fields in the documentation.
How to work with JSON fields?
See Json field section.
How do I add styles or classes to fields or components?
How to use field reactivity?
General information about field reactivity.
The option of using reactivity as an example of Slug field.
How do I set up access rights for different user roles?
On the topic of authorization, read the relevant section of documentation.
To integrate role-based access control in MoonShine, you can use the third-party package moonshine-roles-permissions.
How to properly use resource events (beforeCreating, afterCreated etc.)?
How do I set up filtering in a resource?
How to implement drag and drop sorting records?
TableBuilder component has reorderable() method, which adds the ability to sort rows by dragging.
Here is a recipe for implementing drag-and-drop sorting in a resource.
How can I customize the appearance of the admin panel?
There are many ways to change the appearance of layouts in MoonShine. Read the “Appearance” sections in documentation.
How do I save the authorized user's ID when creating an entry?
In the following example, the current authenticated user is assigned as the author by default.
public function formFields(): array{ return [ // ... BelongsTo::make('Author', resource: UserResource::class) ->default( request()->user() ), ];}
You can also add a hidden field and fill it in with the user ID value from the request.
Hidden::make('Author') ->fill( auth()->id() )
Also in the Model Resource > Events section shows an example of adding a field to a request via events.
What is the correct way to work with fractional numbers in the Number field?
It is enough to specify the required step using the step() method, for example "0.01".
How can I set up asynchronous search in Select fields?
See the Select section.
How can I change or hide the menu items depending on the user's rights?
See the recipe.
How can I change the date format in the Date fields?
See the Date field section.
How do I change the logo and favicon in the admin panel?
The logo can be changed in configuration.
The favicon can be replaced in the Layout
in the Favicon component.
How to implement multiple file uploads?
See File field section.
How do I set up data import/export to CSV or Excel?
See Import /Export section.
How to work with Markdown fields in MoonShine?
You can use moonshine-software/easymde package.
How to customize breadcrumbs in MoonShine?
Breadcrumbs can be redefined on individual pages.
Recipe, how to change breadcrumbs from a resource for individual pages.
How do I remove mass actions and checkboxes from the index page?
How do I set up a global search in MoonShine?
How to implement custom input fields in MoonShine?
It is enough to extend the base class Field
or the class of any of the available fields and add/redefine the functionality you need.
For example, connect another view.
How to use QueryTags in MoonShine?
How do I make or replace buttons in FormBuilder?
components/form-builder#buttons.
How can I adjust the display of fields depending on the value of another field?
fields/basic-methods#show-when.
How can I change or delete the standard action buttons in a resource?
How to work with Enum fields in MoonShine?
How do I add custom pages to MoonShine?
How to work with soft delete in MoonShine?
There is also an article with a more detailed description.
How do I set up custom routes in MoonShine?
How to work with the Switcher field in forms and filters?
Switcher
is the same Checkbox
, only in a different visual design.
How to implement custom authentication in MoonShine?
security/authentication#customization.
How can I adjust the Badge display depending on the value?
Fields have a badge()
method that can accept a closure that returns a color code: fields/basic-methods#badge.
Also see the Enum section.