Reputation: 1004
I have a problem with updating the following table
And the data come from the view like below
this is my code
foreach ($request->input('class_teacher') as $key=>$value){
$class_teacher =ClassTeacher::where('class_id',$id);
$class_teacher->class_id = $classes->class_id;
$class_teacher->teacher_id =$value;
$class_teacher->save();
}
after the update I want it to be like this one
Upvotes: 1
Views: 652
Reputation: 24
First delete the relationships, then recreate them all again.
ClassTeacher::where('class_id',$id)->whereIn('teacher_id', $request->input('class_teacher'))->delete();
foreach ($request->input('class_teacher') as $key=>$value){
$class_teacher =new ClassTeacher;
$class_teacher->class_id = $classes->class_id;
$class_teacher->teacher_id =$value;
$class_teacher->save();
Upvotes: 1