Attach with dynamic fields
Hi, I'm trying to make a RelationManager attach, when a "Trigger" field is selected, show the fields of that Trigger, defined in an json options field (another option is a TriggersAttribute model). For example, the Trigger 'Signup', would have the fields:
'time_ago' (text)
country_id' (numeric)
Another Trigger will need different fields to be filled in. Let's say it is to make dynamic the attributes that I store in the pivot in a json.
I have come up with this code:
->headerActions([
AttachAction::make()
->preloadRecordSelect()
->recordTitleAttribute('name')
->form(function (AttachAction $action) {
return [
$action->getRecordSelect()
->reactive()
->afterStateUpdated(function (callable $set, $state) {
$trigger = Trigger::find($state);
if (!$trigger || empty($trigger->options)) {
return;
}
foreach ($trigger->options as $field => $config) {
if ($config['type'] == 'text') {
$set('dynamicFields.'.$field, $config);
}
}
}),
TextInput::make('dynamic_name')
->label('Name')
->visible(fn(callable $get) => $get('dynamicFields.name.type') === 'text'),
];
}),
])
But I don't get what I'm looking for, could you help me?3 Replies
Sorry, this is the code I have now
It's cold here
Could you hack away around this?