FilamentF
Filament7mo ago
Nedwor

CreateUsingOption() not getting relation in $data

Hello everyone, I am stuck here. I have in a TableRepeater in which I have a Select with a createOptionForm and a createOptionUsing. So it kind of works... I mean, createOptionForm creates the form, I can fill it up normally and when I save it creates the new record. Good BUT, in this record I want to save, I have a relationship (in a Section) and this relation is not saved at all. Neither I get the relation in $data that I pass to createOptionUsing($data).

Select::make('relation_type_id')
  ->options(function () use () {
      return [blablabla]
  })
  ->createOptionForm(function ($form) {
      $form->model(MyDataType::class);
      // getCreateOptionForm is just a function inside a Trait (put on a resrouce) which returns static::form($form)
      return MyDataTypeResource::getCreateOptionForm($form);
  })
  ->createOptionUsing(function (array $data) {
      $model = MyDataType::class;
      $record = $model::create($data);
      return $record->id;
  })
  ->searchable()
  ->required(),


So in this code if I
dd($data)
in createOptionUsing I get something like this :

array:4 [
"name" => "aaaaaaa"
"color" => "#a15353"
"description" => null
"context" => "family"
]

But this is only the half of my form, I was expecting something like

array:4 [
"name" => "aaaaaaa"
"color" => "#a15353"
"description" => null
"context" => "family"
"reverse" => [
"name" => "blablabla"
...
]
]

Does someone can see any reason why I get half of my $data ?
Solution
I think you can use ->dehydrated() to get the values instead of saving them directly.
Was this page helpful?