我在 Laravel 5.2 中有 ManyToMany 关系:
用户型号:
public function Lessons(){
return $this->belongsToMany('App\Lesson')->withPivot('created_by', 'number_of_lessons','id');
}
但是,我想在数据透视表中保存多个课程,如下所示:
$output = [];
$output[] = $this->RidingCourses()->wherePivot('created_by',1)->first()->pivot->number_of_lessons = 200;
$output[] = $this->RidingCourses()->wherePivot('created_by',2)->first()->pivot->number_of_lessons = 200;
$this->RidingCourses()->saveMany([$output]);
数据透视记录正在更新,但在数据透视表中还创建了两条空白记录。所以我的问题是,如何正确保存多个多对多相关模型?