0

我在 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]);

数据透视记录正在更新,但在数据透视表中还创建了两条空白记录。所以我的问题是,如何正确保存多个多对多相关模型?

4

1 回答 1

0

检查“created_by”、“number_of_lessons”、“id”是否可以在您的模型中填写?您是否尝试更新现有记录?

如果这些列是可填充的并且仍然面临相同的问题,则使用 save 或 create 方法代替 saveMany。

我也面临同样的问题并在 Laracast 中提出了同样的问题,但还没有得到任何回应。

我认为这是 Laravel 5.2 中的一个错误。

于 2016-05-11T11:18:49.997 回答