【发布时间】:2016-09-06 14:33:51
【问题描述】:
我在 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]);
数据透视记录正在更新,但数据透视表中还创建了两条空白记录。所以我的问题是,如何正确保存多个 ManyToMany 相关模型?
【问题讨论】:
标签: eloquent many-to-many laravel-5.2 savemany