Relationship record not automatically created in resource View page action
I have an Order model with relationship of hasOne Training model.
After order creation, there is an action modal in the ViewOrder page that prompt user to enter remarks.
Upon action submission, I was hoping a related Training record would be created if not existed yet, else its remarks field should be updated but alas only updating existing record works. I've simplified the code below for review purpose.
Order class
Training class
4 Replies
Are there any fields from Training embedded in the Order form? If not, there's nothing signaling Filament to create the related item. One option would be to create it manually in a model observer or in a lifecycle hook like afterCreate():
https://filamentphp.com/docs/3.x/panels/resources/creating-records#lifecycle-hooks
Or maybe directly in the
->action()
? You could check if the related item exists before trying to update it.Okay, I tried doing it like this, but no result
oh i checked the wrong thing, should be
$training->exists()
. You think my method is gud enough?
or is it better to just auto create related records in orderObserver created eventYes, I think I would do it this way personally, with
action()
... model observers are powerful but more complicated to keep track of.