How to pass a section_id parameter to a action when creating a record?

I'm trying to create a Field that belongs to a Section. I have a create action with a form, and I need to pass the section_id to this form when creating a new Field.
9 Replies
LeandroFerreira
where is the section id? Please simplify your code to focus on the issue..
Manuk Minasyan
Manuk MinasyanOP2w ago
public function createFieldAction(): Action
{
return Action::make('createField')
->size(ActionSize::ExtraSmall)
->label('Create Field')
->form(function(){
return [
Forms\Components\TextInput::make('name')
->required()
->live(onBlur: true)
->maxLength(50),
];
})
->fillForm([
'entity_type' => $this->currentEntityType,
])
->mutateFormDataUsing(function (array $data): array {
if (Utils::isTenantEnabled()) {
$data[config('custom-fields.column_names.tenant_foreign_key')] = Filament::getTenant()?->id;
}

$data['custom_field_section_id'] = null; // I need add section_id here

return $data;
})
->action(fn(array $data) => CustomField::create($data))
->slideOver();
}
public function createFieldAction(): Action
{
return Action::make('createField')
->size(ActionSize::ExtraSmall)
->label('Create Field')
->form(function(){
return [
Forms\Components\TextInput::make('name')
->required()
->live(onBlur: true)
->maxLength(50),
];
})
->fillForm([
'entity_type' => $this->currentEntityType,
])
->mutateFormDataUsing(function (array $data): array {
if (Utils::isTenantEnabled()) {
$data[config('custom-fields.column_names.tenant_foreign_key')] = Filament::getTenant()?->id;
}

$data['custom_field_section_id'] = null; // I need add section_id here

return $data;
})
->action(fn(array $data) => CustomField::create($data))
->slideOver();
}
@foreach ($this->sections as $section)
<x-filament::section x-sortable-item="{{ $section['id'] }}" compact>
<x-slot name="footerActions">
<x-filament::button size="sm" wire:click="mountAction('createFieldAction', { sectionId: 12345 })">
Create field
</x-filament::button>
</x-slot>
</x-filament::section>
@endforeach
@foreach ($this->sections as $section)
<x-filament::section x-sortable-item="{{ $section['id'] }}" compact>
<x-slot name="footerActions">
<x-filament::button size="sm" wire:click="mountAction('createFieldAction', { sectionId: 12345 })">
Create field
</x-filament::button>
</x-slot>
</x-filament::section>
@endforeach
Manuk Minasyan
Manuk MinasyanOP2w ago
How to do this? I couldn't find anything in the documentation. When I use action without form, this is simple to pass arguement and get it, but with forms this is difficult. in ->action method how to get form data and arguments that I passed from the view?
LeandroFerreira
DId you check my link?
Manuk Minasyan
Manuk MinasyanOP2w ago
Are you mean create different component for this purpose? Am I right?
LeandroFerreira
just use InteractsWithActions InteractsWithForms and render the action in your view 🤷‍♂️
Manuk Minasyan
Manuk MinasyanOP2w ago
I will try it. Thank you

Did you find this page helpful?