F
Filament16mo ago
X7Ryan

Add custom data when creating using Table Create Action with Relation Manager

I need to add some custom data to the user's input before saving when creating a new entry from the Table Create Action. Specifically I want to add the current users ID. I tried using the using() method but that seemed to break other things. Specifically I am creating from a Relation Manager and the using() method means it does not get the parent record ID when creating. Is there another approach that will work with a Relation Manager like this?
1 Reply
torriv
torriv16mo ago
were you able to solve this? if not, here is how i add some custom data when attaching from relation manager. (Should work with create action too)
->headerActions([
Tables\Actions\AttachAction::make()
->label('Legg til øvelse i program')
->recordSelect(fn () => Select::make('recordId')->label('Øvelse')
->options(WorkoutExercise::all()->pluck('exercise_name', 'id'))
->searchable()
->preload()
->createOptionForm([
Forms\Components\TextInput::make('exercise_name')->label('Øvelse'),
])
->createOptionUsing(function ($data){
$exercise = WorkoutExercise::create($data);
return $exercise->getKey();
})
)
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('description'),
Forms\Components\TextInput::make('repetitions')->required(),
Forms\Components\TextInput::make('sets')->required(),
Forms\Components\TimePicker::make('rest')
->native(false)
->secondsStep(10)
->displayFormat('i:s')
->format('H:i:s')
->required(),
Forms\Components\Hidden::make('id')
])
])
->headerActions([
Tables\Actions\AttachAction::make()
->label('Legg til øvelse i program')
->recordSelect(fn () => Select::make('recordId')->label('Øvelse')
->options(WorkoutExercise::all()->pluck('exercise_name', 'id'))
->searchable()
->preload()
->createOptionForm([
Forms\Components\TextInput::make('exercise_name')->label('Øvelse'),
])
->createOptionUsing(function ($data){
$exercise = WorkoutExercise::create($data);
return $exercise->getKey();
})
)
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('description'),
Forms\Components\TextInput::make('repetitions')->required(),
Forms\Components\TextInput::make('sets')->required(),
Forms\Components\TimePicker::make('rest')
->native(false)
->secondsStep(10)
->displayFormat('i:s')
->format('H:i:s')
->required(),
Forms\Components\Hidden::make('id')
])
])
you add the custom data in
->form()
->form()

Did you find this page helpful?