Use sync
https://laravel.com/docs/8.x/eloquent-relationships#syncing-associations
You should pass a list of IDs to it, it remove any relationship that isn't in that list and will create new records for new IDs.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello, I have 3 tables:
I can create or remove new Persons. But i don't understand how to update multiselected languages (something remove or something update).
In model Person.php i use this code:
public function spokenLanguages()
{
return $this->hasManyThrough(
Language::class,
PersonsLanguages::class,
'person_id',
'id',
'id',
'lang_id'
);
}
In controllerPersonController.php:
public function update(UpdatePersonRequest $request, $id)
{
$person = Person::findOrFail($id);
$person->update($request->all());
return response()->json('Successfully Updated');
}
I don't understand how update spoken languages from table "persons_languages". Can somebody explain me, please?
Gr
Please or to participate in this conversation.