Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ictoplossing's avatar

How to update and/of create related model?

Hello, I have 3 tables:

  1. Table "persons" with multiple select (spoken languages)
  2. Table "languages" with 3 columns: id (auto increment), lang_name (ex., "English", "Dutch").
  3. Table "persons_languages" with 3 columns: id, person_id (id of person from table "person", foreign key), lang_id (id of language from table "language", foreign key).

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

0 likes
8 replies
ictoplossing's avatar

@MohamedTammam yes, thank you. I mean i don't know how to catch and update this one specific field "spokenLanguages"

In this code i use $person->update($request->all()); (catch all fields). But how to put this one specific field?

$person->spokenLanguages()->sync([1, 2, 3]);

How to put in place of [1, 2, 3] array of id's of languages?

Gr

ictoplossing's avatar

@MohamedTammam hi, I use in api.php:

Route::resources([
    'appointments' => \App\Http\Controllers\AppointmentController::class,
    'persons' => \App\Http\Controllers\PersonController::class
]);

PersonEdit.vue:

<v-autocomplete
	chips
	clearable
	deletable-chips
	multiple
	v-model="currentPerson.spoken_languages"
	:items="allLanguages"
	:item-text="item =>`${item.name} (${item.local_name})`"
	item-value="id"
	persistent-hint
	prepend-icon="mdi-message-outline"
	label="Spreektalen">
</v-autocomplete>

and methode:

updatePerson() { /* update Person */
	this.axios
		.patch(`/api/persons/${this.$route.params.id}`, this.currentPerson)
		.then(response => {
			this.$router.push({name: 'viewPerson'});
			this.showAlertUpdated()
		})
		.catch(error => console.log(error))
		.finally(() => this.loading = false)
}

I don't understand how to put newly created or remove languages.

Gr

ictoplossing's avatar

@MohamedTammam , have got an error:

Call to undefined method Illuminate\Database\Eloquent\Relations\HasManyThrough::sync()
public function update(UpdatePersonRequest $request, $id)
{
	$person = Person::findOrFail($id);
	$person->spokenLanguages()->sync($request->spoken_languages);
	$person->update($request->all());

	return response()->json('Successfully Updated');
}

Any idea?

Please or to participate in this conversation.