Updating rows of a belongsToMany relationship
Is there a possibility to update the rows of the pivot table instead of deleting the row. For example I have 2 Table:
Table1 ( group )
ID
NAME
CHALLENGE_ID
Table2 (group_users -> pivot)
ID
GROUP_ID
USER_ID
IS_GROUP
When I do it as follow:
The record will be deleted from the pivot when I remove a user, instead I would like to update the updated_at for example.
Table1 ( group )
ID
NAME
CHALLENGE_ID
Table2 (group_users -> pivot)
ID
GROUP_ID
USER_ID
IS_GROUP
public function users(): BelongsToMany
{
return $this->belongsToMany(TenantUser::class, 'group_users', 'group_id', 'user_id')->withTimestamps();
} public function users(): BelongsToMany
{
return $this->belongsToMany(TenantUser::class, 'group_users', 'group_id', 'user_id')->withTimestamps();
}When I do it as follow:
protected function getFormSchema(): array
{
return [
TextInput::make('name')
->label('Name')
->required()
->autofocus()
->unique('groups', fn(): string => !isset($this->group) ? 'name' : 'id'),
Select::make('challenge_id')
->label('Challenge')
->hint('Optional')
->searchable()
->options(function (): Collection {
return Challenge::where('type', 'group')
->whereIn('id', tenant()->get_challenges()->pluck('id'))
->pluck('title', 'id');;
}),
Select::make('users')
->label('Users')
->multiple()
->searchable()
->relationship('users', 'full_name')
->preload(),
];
}
public function submit()
{
$state = $this->form->getState();
if ($this->group) {
$this->group->update($state);
} else {
$this->group = TenantGroup::create($state);
$this->form->model($this->group)->saveRelationships();
}
} protected function getFormSchema(): array
{
return [
TextInput::make('name')
->label('Name')
->required()
->autofocus()
->unique('groups', fn(): string => !isset($this->group) ? 'name' : 'id'),
Select::make('challenge_id')
->label('Challenge')
->hint('Optional')
->searchable()
->options(function (): Collection {
return Challenge::where('type', 'group')
->whereIn('id', tenant()->get_challenges()->pluck('id'))
->pluck('title', 'id');;
}),
Select::make('users')
->label('Users')
->multiple()
->searchable()
->relationship('users', 'full_name')
->preload(),
];
}
public function submit()
{
$state = $this->form->getState();
if ($this->group) {
$this->group->update($state);
} else {
$this->group = TenantGroup::create($state);
$this->form->model($this->group)->saveRelationships();
}
}The record will be deleted from the pivot when I remove a user, instead I would like to update the updated_at for example.