php - Save relationship with multiple columns -
i have laravel application , migrating old database new database. have made changes in tables, added new tables , deleted tables. have made migration script this.
i've user. user assigned school , group. in old database user assigned 1 school , 1 group. so, had column in users table named 'group_id' , column named 'school_id'.
now, made mapping table (i guess right english language word this), because of user should assigned multiple schools , multiple groups.
users table:
id | username | password | more...
schools table:
id | name | address | more...
groups table:
id | name | alias | more...
group_user table: id | group_id | user_id | school_user_id user_school table: id | user_id | school_id
as can see, added id of school_user relation mapping table, because of makes easier work in application. if remove user school, can foreign key remove group_user relation. reason simple: user have assigned school, otherwise can not in group.
my laravel controller saving user is:
$user = new user; $user->id = $gebruiker->id; $user->username = $gebruiker->gebruikersnaam; $user->password_md5 = $gebruiker->wachtwoord; $user->firstname = $gebruiker->voornaam; $user->prefix = $gebruiker->tussenvoegsel; $user->lastname = $gebruiker->achternaam; $user->phonenumber = $gebruiker->telefoonnummer; $user->emailaddress = $gebruiker->email; $user->gender = ($gebruiker->geslacht == 'man' ? 'male' : 'female'); $user->birthdate = $gebruiker->geboortedatum; $user->save(); $user->groups()->sync(array('group_id' => $gebruiker->groep_id)); $user->schools()->sync(array($gebruiker->school_id));
my user model has 2 methods:
public function schools() { homecoming $this->belongstomany('school'); } public function groups() { homecoming $this->belongstomany('group'); }
i can save relationship schools. easy because of mapping table school_user has 2 columns. laravel trick me. but, in group_user table i've 3 columns
i tried create controller saving relationship this:
$user->groups()->sync(array('group_id' => $gebruiker->groep_id, 'school_id' => $gebruiker->school_id));
but, unfortunately, not work. can tell me do?
edit: excuse me! did not inquire right question. have made edit post, have made italic.
you need specify additional columns in pivot table when define relation. so:
public function groups() { homecoming $this->belongstomany('group')->withpivot(['id','school_user_id']); }
see docs here.
update:
you should using attach
, detach
, rather sync
, associated users groups , schools. sync
delete models intermediary pivot table not in array pass it—meaning in existing code, $user->schools()->sync(array($gebruiker->school_id))
delete other schools user associated with. allow user associated 1 school. same goes groups, if utilize sync
groups.
so after save user $user->save()
, run:
$user->schools()->attach($gebruiker->school_id);
then, id of entry added school_user pivot table:
$school_user_id = $user->schools()->wherepivot('school_id', $gebruiker->school_id)->first()->pivot->id;
finally, attach user , group, , set school_user id can utilize foreign key allow cascading delete:
$user->groups()->attach($gebruiker->groep_id, array('school_user_id' => $school_user_id));
update 2: turns out can utilize sync
rather attach
update pivot table when save user (and, in fact, should), long set 2nd attribute of sync
false
. doing disables detaching behavior of sync
, add together new school_user connection user, not delete of other school connections.
$user->schools()->sync([$gebruiker->school_id], false);
the benefit of doing permits users connected multiple schools, prevents duplicate entries in pivot table same school_user combination. normally, kind of integrity check isn't big issue pivot table, since detach
clean duplicate entries. since you'll making relationship between pivot table , group_user table, , relying on cascading deletes foreign key, thought create sure pivot table stays clean possible.
php mysql laravel eloquent
No comments:
Post a Comment