$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
useApp\MoonShine\Resources\ProfileResource;
useMoonShine\Laravel\Fields\Relationships\HasOne;
HasOne::make(
'Profile',
'profile',
resource: ProfileResource::class
)
use App\MoonShine\Resources\ProfileResource;
use 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).
use MoonShine\Laravel\Fields\Relationships\HasOne;
HasOne::make('Profile', 'profile')
useMoonShine\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')
useMoonShine\Laravel\Fields\Relationships\HasOne;
HasOne::make('Profile')
use MoonShine\Laravel\Fields\Relationships\HasOne;
HasOne::make('Profile')
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'),
])
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
useMoonShine\Laravel\Resources\ModelResource;
useMoonShine\Traits\Resource\ResourceWithParent;
classPostImageResourceextendsModelResource
{
useResourceWithParent;
// ...
}
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';
}
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')
)