Optionally adding a morthOne relationship for the resource form
There is an Item model with a morphOne relationship to the Attachments model.
In the ItemResource form when creating an Item, the ability to add an attachment for the item is also added, but it should be optional, not mandatory.
The problem is that if I don't load the attachment, an error is returned because when I create Item at the same time it tries to create the Attachment relationship on null
I would appreciate if anyone advise me If there is a way to add an attachment optionally ItemResource form?
Section::make('Attachments')->schema([
FileUpload::make('url')
->required()
->downloadable()
->label('File'),
])->relationship('attachments')
5 Replies
If the attachment is optional, you should remove the required() right? 🤔
This is a temporary solution until I figure out how to fix the problem, the validation rule required() should ensure that users attach the file so they don't see the error
Can you show me the error ?
@Dos have you tried add nullable in the url column?
If the values can be empty I always use nullable in the column or make a defaults value.