#Accessing relationship data in form lifecycle hook

14 messages · Page 1 of 1 (latest)

rain nimbus
#

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?

graceful hawkBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

vast shadow
#

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

rain nimbus
#

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

vast shadow
#

So the only reason why you place the field there is for design reasons?

rain nimbus
#

Basically, yes. If I place it outside the relationship, I should be able to access it via $data.

vast shadow
#

Yes

#

Can't you get the same/similar look with the field outside? 🤔

rain nimbus
#

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

agile granite
#

you would need to travese with ../ or trafficRegulationOrder/traffic_regulation to get the value