Relation Manager

In relation manager for a resource, if the relation is hasOne, how do I stop creation of a another record if one already exists? Thanks in advance for any help.
10 Replies
Dennis Koch
Dennis Koch2y ago
You shouldn't use a Relation Manager for HasOne. Use a Layout field like Fieldset
Anish
AnishOP2y ago
But then I have a different issue. Some instances do not have the relation, i.e., it returns null. In such cases, updating fails saying that some fields are required.
Dennis Koch
Dennis Koch2y ago
Show your code
Anish
AnishOP2y ago
IssueResource


public static function form(Form $form): Form
{



return $form
->schema([

Forms\Components\Fieldset::make('Information')
->schema([
Forms\Components\TextInput::make('volume')
->required(),

Forms\Components\Select::make('series')
->options(SankhyaSeries::getAsSelectOptions()),

Forms\Components\Select::make('parts')
->options($partOptions)
->multiple()
,

Forms\Components\Select::make('year')
->options(array_combine($yearOptions, $yearOptions))
->default(now()->year)
->searchable()
->required(),

Forms\Components\Select::make('month')
->options(Month::getAsSelectOptions())
->required(),
])
->columns(2),


Forms\Components\Fieldset::make('Special Issue')
->relationship('special_issue')

->schema([
Forms\Components\TextInput::make('name')

->label('Special Issue Name')
->maxLength(1000)
->helperText('Input the name of special issue, if this is one such')
->columnSpan(2),
]),

]);
}



public static function form(Form $form): Form
{



return $form
->schema([

Forms\Components\Fieldset::make('Information')
->schema([
Forms\Components\TextInput::make('volume')
->required(),

Forms\Components\Select::make('series')
->options(SankhyaSeries::getAsSelectOptions()),

Forms\Components\Select::make('parts')
->options($partOptions)
->multiple()
,

Forms\Components\Select::make('year')
->options(array_combine($yearOptions, $yearOptions))
->default(now()->year)
->searchable()
->required(),

Forms\Components\Select::make('month')
->options(Month::getAsSelectOptions())
->required(),
])
->columns(2),


Forms\Components\Fieldset::make('Special Issue')
->relationship('special_issue')

->schema([
Forms\Components\TextInput::make('name')

->label('Special Issue Name')
->maxLength(1000)
->helperText('Input the name of special issue, if this is one such')
->columnSpan(2),
]),

]);
}

Dennis Koch
Dennis Koch2y ago
?
Anish
AnishOP2y ago
Sorry, pressed enter instead of shift enter. The special_issue is the HasOne Relation. Not all issues are special, only some of them.
Dennis Koch
Dennis Koch2y ago
You could add a Toggle to enable the special_issue. Or use ->saveRelationshipsUsing() to only save when name is not empty
Anish
AnishOP2y ago
Yes, I have thought about using a toggle to hide / show it. But did want to do it, if there is another cleaner solution. How does one use saveRelationshipsUsing? Should it used on the form component? I am unable to find documentation on that.
Dennis Koch
Dennis Koch2y ago
Yes, you use it on the fieldset and it overwrites the saving logic. Check the EntanglesStateWithSingularRelationship::relationship() method for the default implementation. It's a bit complex though. Not sure whether we have a cleaner way. I guess a toggle would be cleaner.
Anish
AnishOP2y ago
Ok. Thanks for the help.
Want results from more Discord servers?
Add your server