F
Filamentβ€’6mo ago
Nuekrato

How to get $data from table form action?

I built a rather complex action in my table which looks like this:
->actions([
Action::make('permissions')
->form(function (User $record) {
$checkboxLists = [];
foreach ($this->project->targets as $target) {
$checkboxLists[] = CheckboxList::make('directPermissions')
->label($target->name)
->relationship(
titleAttribute: 'name'
)
->options(function () use ($target) {
$permissions = [];
foreach ($this->project->directPermissions()->where('destination_id', $target->id)->get() as $permission) {
$permissions[$permission->id] = $permission->name;
}
return $permissions;
});
}
return $checkboxLists;
})
->fillForm(function ($record) {
return $record->toArray();
})
->model(function ($record) {
return $record;
})
->action(function (array $data, $record, RelationManager $livewire) {
logger()->debug('Permissions data', $data);
})
])
->actions([
Action::make('permissions')
->form(function (User $record) {
$checkboxLists = [];
foreach ($this->project->targets as $target) {
$checkboxLists[] = CheckboxList::make('directPermissions')
->label($target->name)
->relationship(
titleAttribute: 'name'
)
->options(function () use ($target) {
$permissions = [];
foreach ($this->project->directPermissions()->where('destination_id', $target->id)->get() as $permission) {
$permissions[$permission->id] = $permission->name;
}
return $permissions;
});
}
return $checkboxLists;
})
->fillForm(function ($record) {
return $record->toArray();
})
->model(function ($record) {
return $record;
})
->action(function (array $data, $record, RelationManager $livewire) {
logger()->debug('Permissions data', $data);
})
])
This works perfectly fine and correctly sets the user's permissions in their project. The problem is that I am just not able to get the $data in the ->action -method. After submitting the form the $data seems to be empty: [2024-05-08 08:31:58] production.DEBUG: Permissions data So the ->action-method is triggered correctly but is not getting any data back from the form?
7 Replies
Dennis Koch
Dennis Kochβ€’6mo ago
Relationships save on their own so the data is not included. I think you can use ->dehydrated(true)?!
Nuekrato
Nuekratoβ€’6mo ago
Mh.. I added it to the form but it doesn't make any difference. The $data is still empty.
->form(function (User $record) {
$checkboxLists = [];
foreach ($this->project->targets as $target) {
$checkboxLists[] = CheckboxList::make('directPermissions')
->dehydrated(true)
->label($target->name)
->relationship(
titleAttribute: 'name'
)
->options(function () use ($target) {
$permissions = [];
foreach ($this->project->directPermissions()->where('destination_id', $target->id)->get() as $permission) {
$permissions[$permission->id] = $permission->name;
}
return $permissions;
});
}
return $checkboxLists;
})
->form(function (User $record) {
$checkboxLists = [];
foreach ($this->project->targets as $target) {
$checkboxLists[] = CheckboxList::make('directPermissions')
->dehydrated(true)
->label($target->name)
->relationship(
titleAttribute: 'name'
)
->options(function () use ($target) {
$permissions = [];
foreach ($this->project->directPermissions()->where('destination_id', $target->id)->get() as $permission) {
$permissions[$permission->id] = $permission->name;
}
return $permissions;
});
}
return $checkboxLists;
})
I tried both true and false and there is no difference at all. I just need to dispatch a custom event with the relationship data - that's why I need to "hook" into the submit action.
Dennis Koch
Dennis Kochβ€’6mo ago
There was a similar question here just some days ago. Thought the solution was using dehydrated().
Nuekrato
Nuekratoβ€’6mo ago
Mh. Sorry I didn't see that question. I will try to search for it. But in this case dehydrated() does not seem to make any difference.
Dennis Koch
Dennis Kochβ€’6mo ago
Hm. Can't find it either πŸ™ˆ
Nuekrato
Nuekratoβ€’6mo ago
Nevermind. I found another way by just using the $record in the action directly and get the relationship after saving. So I can use that data to dispatch an Event:
->action(function ($record, $livewire) {
logger()->debug('Permissions', $record->projectPermissions($livewire->ownerRecord)->get()->toArray());
})
->action(function ($record, $livewire) {
logger()->debug('Permissions', $record->projectPermissions($livewire->ownerRecord)->get()->toArray());
})
LeandroFerreira
LeandroFerreiraβ€’6mo ago
You should add this after ->relationship()
Want results from more Discord servers?
Add your server