Fields

HasOne

Basics Fields Parent-ID


Basics

The HasOne field is designed to work with the relation of the same name in Laravel and includes all the basic methods.

To create this field, use the static make() method.

HasOne::make( Closure|string $label, ?string $relationName = null, Closure|string|null $formatted = null, ?ModelResource $resource = null )
HasOne::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
?ModelResource $resource = null
)

$label - label, field header, $relationName - name of the relationship, $resource - the model resource referenced by the relation

The $formatted parameter is not used in the HasOne field!

The presence of the model resource referenced by the relation is mandatory!
The resource also needs to be registered with the service provider MoonShineServiceProvider in the method menu() or resources(). Otherwise, there will be a 404 error.

use MoonShine\Fields\Relationships\HasOne; //... public function fields(): array { return [ HasOne::make('Profile', 'profile', resource: new ProfileResource()) ]; } //...
use MoonShine\Fields\Relationships\HasOne;
 
//...
 
public function fields(): array
{
return [
HasOne::make('Profile', 'profile', resource: new ProfileResource())
];
}
 
//...

has_one has_one_dark

If you do not specify $relationName, then the relation name will be determined automatically based on $label.

use MoonShine\Fields\Relationships\HasOne; //... public function fields(): array { return [ HasOne::make('Profile', resource: new ProfileResource()) ]; } //...
use MoonShine\Fields\Relationships\HasOne;
 
//...
 
public function fields(): array
{
return [
HasOne::make('Profile', resource: new ProfileResource())
];
}
 
//...

You can omit $resource if the model resource matches the name of the relationship.

use MoonShine\Fields\Relationships\HasOne; //... public function fields(): array { return [ HasOne::make('Profile', 'profile') ]; } //...
use MoonShine\Fields\Relationships\HasOne;
 
//...
 
public function fields(): array
{
return [
HasOne::make('Profile', 'profile')
];
}
 
//...

Fields

The fields() method allows you to specify which fields will participate in preview or in building forms.

fields(Fields|Closure|array $fields)
fields(Fields|Closure|array $fields)
use MoonShine\Fields\Relationships\HasOne; use MoonShine\Fields\Phone; use MoonShine\Fields\Text; //... public function fields(): array { return [ HasOne::make('Contacts', resource: new ContactResource()) ->fields([ Phone::make('Phone'), Text::make('Address'), ]) ]; } //...
use MoonShine\Fields\Relationships\HasOne;
use MoonShine\Fields\Phone;
use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
HasOne::make('Contacts', resource: new ContactResource())
->fields([
Phone::make('Phone'),
Text::make('Address'),
])
];
}
 
//...

has_one_preview

has_one_preview_dark

Parent ID

If the relationship has a resource and you want to get the ID of the parent element, then you can use the ResourceWithParent trait.

use MoonShine\Resources\ModelResource; use MoonShine\Traits\Resource\ResourceWithParent; class PostImageResource extends ModelResource { use ResourceWithParent; //... }
use MoonShine\Resources\ModelResource;
use MoonShine\Traits\Resource\ResourceWithParent;
 
class PostImageResource extends ModelResource
{
use ResourceWithParent;
 
//...
}

When using a trait, it is necessary to define methods:

protected function getParentResourceClassName(): string { return PostResource::class; } protected function getParentRelationName(): string { return 'post'; }
protected function getParentResourceClassName(): string
{
return PostResource::class;
}
 
protected function getParentRelationName(): string
{
return 'post';
}

To get the parent ID, use the getParentId() method.

$this->getParentId();
$this->getParentId();

Recipe: saving files HasMany connections in the directory with the parent ID.