Saving relationships on livewire Action

Hey guys, I have a strange situation. I created a livewire component with Filament-Actions. My action looks like this:
public function createAction(): Action
{
return CreateAction::make('create')
->label(__('Add page'))
->modalHeading(__('Add a new page'))
->modalDescription(__('bla'))
->modalSubmitActionLabel(__('Create'))

->form([
TextInput::make('name')
->label(__('Name'))
->required(),
Grid::make()
->schema([
Select::make('status')
->label(__('Status'))
->required()
->options([
0 => 'Hidden',
1 => 'Visible',
]),

Select::make('teams')
->label(__('Teams'))
->required()
->preload()
->multiple()
->searchable()
->relationship('teams', 'name'),
]),

RichEditor::make('content')
->label(__('Content'))
->required(),

Hidden::make('sort')
->default(fn () => Page::count() + 1),
])
->model(Page::class)
->action(
fn (Form $form) => dd($form->getState()) // Getting state - there is no teams relation
);
}
public function createAction(): Action
{
return CreateAction::make('create')
->label(__('Add page'))
->modalHeading(__('Add a new page'))
->modalDescription(__('bla'))
->modalSubmitActionLabel(__('Create'))

->form([
TextInput::make('name')
->label(__('Name'))
->required(),
Grid::make()
->schema([
Select::make('status')
->label(__('Status'))
->required()
->options([
0 => 'Hidden',
1 => 'Visible',
]),

Select::make('teams')
->label(__('Teams'))
->required()
->preload()
->multiple()
->searchable()
->relationship('teams', 'name'),
]),

RichEditor::make('content')
->label(__('Content'))
->required(),

Hidden::make('sort')
->default(fn () => Page::count() + 1),
])
->model(Page::class)
->action(
fn (Form $form) => dd($form->getState()) // Getting state - there is no teams relation
);
}
blade file:
<div>
{{ $this->createAction }}

<x-filament-actions::modals />
</div>
<div>
{{ $this->createAction }}

<x-filament-actions::modals />
</div>
Problem is, that the "teams" relationship will not be saved and is even not available at $form->getState() What am I missing? 😮
Solution:
->dehydrated() in the select will show it in the $data array
Jump to solution
4 Replies
Dennis Koch
Dennis Koch7mo ago
Fields with relationships have the save logic built in and therefore do not pass on the state.
d3v1anX
d3v1anXOP7mo ago
Hey @Dennis Koch, thank you for answering - unfortunately the relationships are not saved automatically on livewire components - at least on my end. All other stuff is saved correctly - but not the relationship. Just for completation: My Page model has relation belongsToMany:
public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class);
}
public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class);
}
and the Team model has also a belongsToMany relation:
public function pages(): BelongsToMany
{
return $this->belongsToMany(Page::class);
}
public function pages(): BelongsToMany
{
return $this->belongsToMany(Page::class);
}
Ah, yeah - sorry for misunderstanding - I missed here in the example, that I want do pass the data through a manual saving process ->
->action(fn(CreatePage $creator, array $data) => $this->create($creator, $data))
->action(fn(CreatePage $creator, array $data) => $this->create($creator, $data))
And there I need the relationship in $data 🙂 but unfortunately I only get the array without the relationship-data:
array:6 [
"name" => "dsadsadsdas"
"status" => "1"
"content" => "<p>asddsdsaads</p>"
"sort" => 6
]
array:6 [
"name" => "dsadsadsdas"
"status" => "1"
"content" => "<p>asddsdsaads</p>"
"sort" => 6
]
Dennis Koch
Dennis Koch7mo ago
You can try accessing the unvalidated data $creator->data. Not sure what's the correct way to get that relationship data.
Solution
LeandroFerreira
LeandroFerreira7mo ago
->dehydrated() in the select will show it in the $data array
Want results from more Discord servers?
Add your server