【发布时间】:2014-05-20 10:19:03
【问题描述】:
是否可以将更多数据保存到多对多数据透视表中?
我的代码:
public function lists() {
return $this->belongsToMany('ShoppingList','shopping_list_ingredients','shopping_list_id','ingredients_id')
->withPivot(array('unit','amount'))
->withTimestamps();
}
反之亦然!
现在,我需要将附加数据添加到数据透视表中。
这是我的保存代码:
$list = new ShoppingList;
$list->user_id = Auth::user()->id;
$list->title = Input::get('recipe_title');
$list->save();
$list->ingredients()->sync(Input::get('ingredient'));
$list->push();
和我的视图代码:
- {{$i->amount}} {{$i->unit}} {{$i->name}} - {{ Form::checkbox('ingredient[]', $i->id) }}<br/>
现在我需要以某种方式将每个 ID 的“数量”和“单位”传递到控制器和枢轴中。目前,它只保存 ID。
我该怎么做?
【问题讨论】:
标签: php laravel pivot relationship eloquent