Answer the question
In order to leave comments, you need to log in
How to synchronize multiple table columns in Laravel relationships?
Hello!
How to synchronize multiple table columns in Laravel relationships?
There is a users table and a User model with declared relationships to the users_params table and the UserParam model:
public function syncParams()
{
return $this->belongsToMany(UserParam::class, 'users_params', 'user_id', 'field1');
}
$user = User::find(2022);
$test = ;
$user->syncParams()->sync($test);
Answer the question
In order to leave comments, you need to log in
I solved the problem like this: I
added another field_id column to the users_params table and made it PRIMARY_KEY like the ID column: id, field_id, user_id, field1, field2
In the User model, I wrote the following:
return $this->belongsToMany(UserParam::class, 'users_params', 'user_id', 'field_id');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question