F
Filament•10mo ago
Clement

Can't receive relationship data on a form createAction

Hi I have a small problem but terrible to investigate on it .. I have this code on my headerActions of my table:
\Filament\Tables\Actions\CreateAction::make('Create Workspace')
->label('Create Workspace')
->modalDescription('Create a new workspace for your organization')
->form([
Grid::make()
->columns(3)
->schema($this->workspaceSchema()),
])
->model(TeamWorkSpace::class)
->using(function (TeamWorkSpace $workspace, array $data): void {
$data['team_id'] = $this->team->id;

$workspace->fill(collect($data)->only(['name', 'team_id', 'available_space', 'slug'])->toArray())->save();
$workspace->metiers()->sync($data['metiers']);
// $data['metiers'] is empty ..

}),
])
\Filament\Tables\Actions\CreateAction::make('Create Workspace')
->label('Create Workspace')
->modalDescription('Create a new workspace for your organization')
->form([
Grid::make()
->columns(3)
->schema($this->workspaceSchema()),
])
->model(TeamWorkSpace::class)
->using(function (TeamWorkSpace $workspace, array $data): void {
$data['team_id'] = $this->team->id;

$workspace->fill(collect($data)->only(['name', 'team_id', 'available_space', 'slug'])->toArray())->save();
$workspace->metiers()->sync($data['metiers']);
// $data['metiers'] is empty ..

}),
])
I also have my workspaceSchema (for my forms), Everything work to create "TeamWorkspace" also when I use the select multiple to create a "metiers" (BelongsToMany relation of TeamWorkspace) I don't have any "metiers" value stored in my $data ... Also when I edit TeamWorkspace with the following code it works perfectly:
EditAction::make('Edit Workspace')
->form([
Grid::make()
->columns(3)
->schema($this->workspaceSchema()),
])
->using(function (TeamWorkSpace $workspace, array $data): void {
$data['team_id'] = $this->team->id;

$workspace->fill(collect($data)->only(['name', 'team_id', 'available_space', 'slug'])->toArray())->save();
}),
EditAction::make('Edit Workspace')
->form([
Grid::make()
->columns(3)
->schema($this->workspaceSchema()),
])
->using(function (TeamWorkSpace $workspace, array $data): void {
$data['team_id'] = $this->team->id;

$workspace->fill(collect($data)->only(['name', 'team_id', 'available_space', 'slug'])->toArray())->save();
}),
I understand that it works because TeamWorkspace is already created .. But how to in CreateAction
2 Replies
Clement
ClementOP•10mo ago
my schema:
protected function workspaceSchema(): array
{
return [
TextInput::make('name')
->columnSpan(2)
->label('WorkSpace Name')
->placeholder('Enter the name of the workspace')
->live(debounce: 500)
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
TextInput::make('available_space')
->columnSpan(1)
->label('Space Available')
->numeric()
->inputMode('decimal')
->placeholder('Enter the space available in the workspace')
->extraInputAttributes([
'min' => 0,
])
->rules(['gt:0']),
TextInput::make('slug')
->readOnly()
->required()
->unique(TeamWorkSpace::class, 'slug', ignoreRecord: true),
Select::make('metiers')
->label('Add jobs access to this workspace')
->relationship('metiers')
->options(
$this->team->metiers()->get()->pluck('name', 'id')
)
->searchable()
->multiple()
->columnSpan(2)
];
}
protected function workspaceSchema(): array
{
return [
TextInput::make('name')
->columnSpan(2)
->label('WorkSpace Name')
->placeholder('Enter the name of the workspace')
->live(debounce: 500)
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
TextInput::make('available_space')
->columnSpan(1)
->label('Space Available')
->numeric()
->inputMode('decimal')
->placeholder('Enter the space available in the workspace')
->extraInputAttributes([
'min' => 0,
])
->rules(['gt:0']),
TextInput::make('slug')
->readOnly()
->required()
->unique(TeamWorkSpace::class, 'slug', ignoreRecord: true),
Select::make('metiers')
->label('Add jobs access to this workspace')
->relationship('metiers')
->options(
$this->team->metiers()->get()->pluck('name', 'id')
)
->searchable()
->multiple()
->columnSpan(2)
];
}
The problem is located with my Select::make('metiers') where i use it for my relation "metiers" When I remove relationship I can get in $data metiers also Multiple is not populated with already linked "metiers" on edit @Dennis Koch ? 🙂
Saade
Saade•10mo ago
1st. Read #✅┊rules to know how to format your code properly. 2nd. Please don't tag people. 3rd. If you're using ->relationship() the relation will be saved automatically for you, hence why there's no metiers in the $data array
Want results from more Discord servers?
Add your server