F
Filamentā€¢2mo ago
aldec

Accessing relationship data in form lifecycle hook

I'm trying to accessing (and possibly unsetting) a form input in the mutateDataBeforeCreate() hook. The form input is in a relationship (via Group::make()->relationship). But accessing the $data variable only shows the inputs from the root resource, all the relationship-related inputs are not there. How can I access them?
8 Replies
Dennis Koch
Dennis Kochā€¢2mo ago
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
aldec
aldecOPā€¢2mo ago
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
Dennis Koch
Dennis Kochā€¢2mo ago
Yes. They are on that field (not the page) So the only reason why you place the field there is for design reasons?
aldec
aldecOPā€¢2mo ago
Basically, yes. If I place it outside the relationship, I should be able to access it via $data.
Dennis Koch
Dennis Kochā€¢2mo ago
Yes Can't you get the same/similar look with the field outside? šŸ¤”
aldec
aldecOPā€¢2mo ago
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')
]),
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
toeknee
toekneeā€¢2mo ago
you would need to travese with ../ or trafficRegulationOrder/traffic_regulation to get the value
aldec
aldecOPā€¢2mo ago
>hidden(fn(Get $get) => $get('trafficRegulationOrder.traffic_regulation') !== 'beigefĆ¼gtem Regelplan') works, layout wise I have to rethink my initial plan

Did you find this page helpful?