#Submitting form from Section header action

1 messages · Page 1 of 1 (latest)

crisp orchid
#

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();
})
stuck urchinBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

astral cosmos
#

$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);
    }
})
crisp orchid
#

@astral cosmos 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();
      })