Update pivot table when editing record
Hi there,
I am trying to update a pivot table record while editing the main record.
I have the following table
team_user
user_id
team_id
operator_id (just a string value I wish to update)
On both user and team I have belongsToMany (M:M relationship).
This is how I fetch the operator_id in the table, using the team_user pivot table
TextColumn::make('operator_id')->label('Oznaka operatera')->sortable()->searchable()
->state(function (User $record): string {
return $record->getUserInTeam()->first()->pivot->operator_id;
}),
How can I acomplish the same so when the record is created, operator_id is filled from the form and when the record is being edited, operator ID gets displayed and can be edited?1 Reply
I managed to save it and make it editable by using
User CreatePage
protected function handleRecordCreation(array $data): Model
...
$tenant->users()->attach($user->id, ['operator_id' => $data['teamUser_operator_id']]);
EditUser
protected function handleRecordUpdate(Model $record, array $data): Model
{
$record->update($data);
$record->getCurrentTeamUser()->update(['operator_id' => $data['teamUser_operator_id']]);
return $record;
}
Got it to display the data correctly on the table, but still having issues on making it sortable... Any help would be appreciated π
TextColumn::make("teamUser.operator_id")->label('Oznaka operatera')
->getStateUsing(fn ($record) => $record->getUserInTeam()->first()->pivot->operator_id),