#Accessing relationship data in form lifecycle hook
14 messages · Page 1 of 1 (latest)
Relation data is saved directly by the fields and therefore not included. If you want to modify that data, there are similar methods on those fileds I think
Mh... the mutateDataBeforeCreate and handleRecordCreation only take $data as argument. Do you mean accessing the form field's data through an entire different method?
Perhaps my approach is not right overall. I have a hasMany relationship and would like to enable the upload of a file via a Document model (of which the main model can have several in total) in a form.
Since I would like to have the field where it is now (in the relationship group) and relationship() does not support hasMany anyway, my idea was to put the upload field there, but remove it before saving and create the content manually in the Document model and then saving the main model with the rest of the fields. But right now I can't seem to access the form fields data of the relationship
Yes. They are on that field (not the page)
So the only reason why you place the field there is for design reasons?
Basically, yes. If I place it outside the relationship, I should be able to access it via $data.
Here is the form part I'm working on:
Group::make()
->relationship('trafficRegulationOrder')
->schema([
Grid::make()->schema([
Select::make('traffic_regulation')
->options(app(RoadworksSettings::class)->getOptionsArray('selection_traffic_regulation'))
->live()
FileUpload::make('upload_traffic_regulation')
->hidden(fn(Get $get) => $get('traffic_regulation') !== 'beigefügtem individuellen Schilderplan')
->preserveFilenames()
->directory('documents')
->downloadable()
->openable()
->previewable()
->label('Individuellen Schilderplan hochladen'),
Select::make('rsa21')
->options(app(RoadworksSettings::class)->getOptionsArray('selection_rsa21'))
->hidden(fn(Get $get) => $get('traffic_regulation') !== 'beigefügtem Regelplan')
]),
There is a dropdown in the left part of the grid and dependent on the selected option I have a second select on the right or this File Upload
Moving it outside of the relationship group seems to break the hidden() conditional
Ah I have to reference the relationship in the hidden() method as well
you would need to travese with ../ or trafficRegulationOrder/traffic_regulation to get the value