在Laravel elequent 5中保存多对多关系?

时间:2015-07-09 07:46:54

标签: php laravel eloquent laravel-5

我有三张表,employeesallowancesemp_allowances

employees

  • ID
  • 名称

allowances

  • ID
  • 标题
  • 金额

emp_allowances

  • employees_id
  • allowances_id
  • 金额

模型函数是Employee

  public function empAllowances() {
    return $this->belongsToMany('App\Model\EmplAllowance', 'emp_allowances', 'employees_id', 'allowances_id');
}

Allowance

 public function employees() {
    return $this->belongsToMany('App\Model\Employee');
}

它在这里工作$emp->empAllowances()->sync([1]);但是当我遵循此方法时,我无法保存amount

如何使用amount保存员工津贴明细?

1 个答案:

答案 0 :(得分:2)

尝试

$emp->empAllowances()->sync([1 => ['amount' => 10]])

搜索Adding Pivot Data When Syncing here