Basics
The HasOne field is designed to work with the same named relationship in Laravel and includes all Basic methods.
To create this field, use the static method make()
.
HasMany::make( Closure|string $label, ?string $relationName = null, Closure|string|null $formatted = null, ModelResource|string|null $resource = null,)
-
$label
- the label, title of the field, -
$relationName
- the name of the relationship, -
$resource
- the model resource that the relationship refers to.
The $formatted
parameter is not used in the HasMany
field!
Having a model resource that the relationship refers to is mandatory.
The resource must also be registered in the MoonShineServiceProvider
service provider in the $core->resources()
method. Otherwise, there will be a 500 error (Resource is required for MoonShine\Laravel\Fields\Relationships\HasOne...).
HasOne::make('Profile', 'profile', resource: ProfileResource::class)
If you do not specify $relationName
, the relationship name will be automatically determined based on $label
(following camelCase rules).
class ProfileResource extends ModelResource{ //...}//...HasMany::make('Profile', 'profile')
You can omit $resource
if the model resource matches the name of the relationship.
class ProfileResource extends ModelResource{ //...}//...HasMany::make('Profile')
Fields
The fields()
method allows you to specify which fields will be involved in the preview or in the form building.
fields(FieldsContract|Closure|iterable $fields)
use MoonShine\UI\Fields\Relationships\HasOne;use MoonShine\UI\Fields\Phone;use MoonShine\UI\Fields\Text; HasOne::make('Contacts', resource: ContactResource::class) ->fields([ Phone::make('Phone'), Text::make('Address'), ])
Parent ID
If the relationship has a resource, and you want to get the parent item's ID, you can use the ResourceWithParent trait.
use MoonShine\Resources\ModelResource;use MoonShine\Traits\Resource\ResourceWithParent; class PostImageResource extends ModelResource{ use ResourceWithParent; //...}
When using the trait, you need to define the following methods:
protected function getParentResourceClassName(): string{ return PostResource::class;} protected function getParentRelationName(): string{ return 'post';}
To get the parent ID, use the getParentId()
method.
$this->getParentId();
Modification
Preview
The modifyTable()
method allows you to change the TableBuilder for the preview.
HasOne::make('Comment', resource: CommentResource::class) ->modifyTable( fn(TableBuilder $table) => $table )
Form
The modifyForm()
method allows you to change the FormBuilder for editing.
HasOne::make('Comment', resource: CommentResource::class) ->modifyForm( fn(FormBuilder $form) => $form->submit('Custom title') )
Redirect after modification
The redirectAfter()
method allows for redirection after saving/adding/deleting.
HasOne::make('Comment', resource: CommentResource::class) ->redirectAfter(fn(int $parentId) => route('home'))