F
Filament4mo ago
lmtc

Submitting form from Section header action

I'm trying to add some additional fields to a form, but it doesn't seem to be submitting. I have this in the action but $record seems to be null like it's not running mount:
->action(function (array $data,Config $record) {
$record->fill($data);
$record->save();
})
->action(function (array $data,Config $record) {
$record->fill($data);
$record->save();
})
3 Replies
Ron
Ron4mo ago
$record will be null on create. If you want to submit the form on both create and edit use $context
->action(function (array $data, Config $record, string $context) {
if ($context === 'edit') {
$record->fill($data);
$record->save();
}
if ($context === 'create') {
Config::create($data);
}
})
->action(function (array $data, Config $record, string $context) {
if ($context === 'edit') {
$record->fill($data);
$record->save();
}
if ($context === 'create') {
Config::create($data);
}
})
lmtc
lmtcOP4mo ago
@Ron I ended up getting it working by redefining the record since it's always the same, not sure if this is the right approach?
->action(function (array $data) {
$this->record=Config::firstOrNew([
'config_type' => 'global_styling',
]);
$existing_data=$this->record->config_value;
$updated_data = array_merge($existing_data[0], $data);
$this->record->config_value = [$updated_data];
$this->record->save();
})
->action(function (array $data) {
$this->record=Config::firstOrNew([
'config_type' => 'global_styling',
]);
$existing_data=$this->record->config_value;
$updated_data = array_merge($existing_data[0], $data);
$this->record->config_value = [$updated_data];
$this->record->save();
})
Ron
Ron4mo ago
@lmtc this looks like a reasonable approach. I am not sure exactly what is happening with the config value array , but if you want all the config value key/value pairs to be editable , you can fill the form with existing data https://filamentphp.com/docs/3.x/actions/modals#filling-the-form-with-existing-data and then use a simpler Config::updateOrCreate() if 'config_type" is unique for the Config model
Want results from more Discord servers?
Add your server