Fields

HasOne

Basics

The HasOne field is designed to work with the same-name relationship in Laravel and includes all Basic methods.

HasOne::make( Closure|string $label, ?string $relationName = null, Closure|string|null $formatted = null, ModelResource|string|null $resource = null, )
HasOne::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 - ModelResource that the relationship refers to.

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

Having a ModelResource 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.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:2] use App\MoonShine\Resources\ProfileResource; use MoonShine\Laravel\Fields\Relationships\HasOne; HasOne::make( 'Profile', 'profile', resource: ProfileResource::class )
 namespaces
use App\MoonShine\Resources\ProfileResource;
use MoonShine\Laravel\Fields\Relationships\HasOne;
 
HasOne::make(
'Profile',
'profile',
resource: ProfileResource::class
)

has_one has_one_dark

If you do not specify $relationName, the relationship name will be automatically determined based on $label (following camelCase rules).

use MoonShine\Laravel\Fields\Relationships\HasOne; HasOne::make('Profile', 'profile')
use MoonShine\Laravel\Fields\Relationships\HasOne;
 
HasOne::make('Profile', 'profile')

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

use MoonShine\Laravel\Fields\Relationships\HasOne; HasOne::make('Profile')
use MoonShine\Laravel\Fields\Relationships\HasOne;
 
HasOne::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)
fields(FieldsContract|Closure|iterable $fields)
// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:4] use App\MoonShine\Resources\ProfileResource; use MoonShine\UI\Fields\Relationships\HasOne; use MoonShine\UI\Fields\Phone; use MoonShine\UI\Fields\Text; HasOne::make('Profile', resource: ProfileResource::class) ->fields([ Phone::make('Phone'), Text::make('Address'), ])
 namespaces
use App\MoonShine\Resources\ProfileResource;
use MoonShine\UI\Fields\Relationships\HasOne;
use MoonShine\UI\Fields\Phone;
use MoonShine\UI\Fields\Text;
 
HasOne::make('Profile', resource: ProfileResource::class)
->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 parent item's ID, you can use the ResourceWithParent trait.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:2] use MoonShine\Laravel\Resources\ModelResource; use MoonShine\Traits\Resource\ResourceWithParent; class PostImageResource extends ModelResource { use ResourceWithParent; // ... }
 namespaces
use MoonShine\Laravel\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'; }
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();

Modification

Preview

The modifyTable() method allows you to change the TableBuilder for the preview.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] use App\MoonShine\Resources\CommentResource; use MoonShine\Laravel\Fields\Relationships\HasOne; use MoonShine\UI\Components\Table\TableBuilder; HasOne::make('Comment', resource: CommentResource::class) ->modifyTable( fn(TableBuilder $table) => $table )
 namespaces
use App\MoonShine\Resources\CommentResource;
use MoonShine\Laravel\Fields\Relationships\HasOne;
use MoonShine\UI\Components\Table\TableBuilder;
 
HasOne::make('Comment', resource: CommentResource::class)
->modifyTable(
fn(TableBuilder $table) => $table
)

Form

The modifyForm() method allows you to change the FormBuilder for editing.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] use App\MoonShine\Resources\CommentResource; use MoonShine\UI\Components\FormBuilder; use MoonShine\Laravel\Fields\Relationships\HasOne; HasOne::make('Comment', resource: CommentResource::class) ->modifyForm( fn(FormBuilder $form) => $form->submit('Custom title') )
 namespaces
use App\MoonShine\Resources\CommentResource;
use MoonShine\UI\Components\FormBuilder;
use MoonShine\Laravel\Fields\Relationships\HasOne;
 
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.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:2] use App\MoonShine\Resources\CommentResource; use MoonShine\Laravel\Fields\Relationships\HasOne; HasOne::make('Comment', resource: CommentResource::class) ->redirectAfter(fn(int $parentId) => route('home'))
 namespaces
use App\MoonShine\Resources\CommentResource;
use MoonShine\Laravel\Fields\Relationships\HasOne;
 
HasOne::make('Comment', resource: CommentResource::class)
->redirectAfter(fn(int $parentId) => route('home'))