F
Filament4mo 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(),
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)
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.
Jump to solution
7 Replies
Dennis Koch
Dennis Koch4mo ago
Maybe share your form. I guess you are using a relationship field and they are saved directly.
Nedwor
NedworOP4mo ago
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\ColorPicker::make('color')
->required(),
Forms\Components\TextInput::make('description')
->maxLength(255),
Forms\Components\Select::make('context')
->options([
'family' => 'Family',
'business' => 'Business'
])
->afterStateUpdated(function ($state, $set) {
$set('reverse.context', $state);
})
->required(),
Forms\Components\Section::make('reverse')
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('description')
->maxLength(255),
Forms\Components\ColorPicker::make('color')
->required(),
Forms\Components\Hidden::make('context')
->default('family')
->required(),
])
->compact()
->relationship('reverse')
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\ColorPicker::make('color')
->required(),
Forms\Components\TextInput::make('description')
->maxLength(255),
Forms\Components\Select::make('context')
->options([
'family' => 'Family',
'business' => 'Business'
])
->afterStateUpdated(function ($state, $set) {
$set('reverse.context', $state);
})
->required(),
Forms\Components\Section::make('reverse')
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('description')
->maxLength(255),
Forms\Components\ColorPicker::make('color')
->required(),
Forms\Components\Hidden::make('context')
->default('family')
->required(),
])
->compact()
->relationship('reverse')
]);
}
Sorry I don't know if it's too long for a snippet or not. But here is my form.
Dennis Koch
Dennis Koch4mo ago
It's the relationship('reverse') part
Solution
Dennis Koch
Dennis Koch4mo ago
I think you can use ->dehydrated() to get the values instead of saving them directly.
Nedwor
NedworOP4mo ago
Oh I will try this, thank you, I'll get back to you ! YES ! It worked ! Thank you ! So relationship automatically findOrCreate and link our records but deactivates the dehydrated ?
Dennis Koch
Dennis Koch4mo ago
Yes, because the values are usually not needed.
Nedwor
NedworOP4mo ago
Alright ! Thank you so much Dennis !

Did you find this page helpful?