【发布时间】:2019-02-09 12:12:23
【问题描述】:
在将此问题标记为重复之前,请查看详细信息 :) 我有一个与自定义列链接的多对多关系相关的问题
我有以下表格
员工
-Id
-brink_id <----- This is third party id I am saving of employee
-name
工作
-id
-brink_id <----- This is third party id I am saving of job
-description
员工工作
-id
-brink_id <----- This is third party id I am saving of relationship for third party
-brink_employee_id
-brink_job_id
在员工模型中我创建了关系
public function jobs()
{
return $this->belongsToMany('App\Models\Job','employee_job','brink_employee_id','brink_job_id');
}
在乔布斯模型中
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function employees()
{
return $this->belongsToMany('App\Models\Employee','employee_job','brink_job_id','brink_employee_id');
}
实际上,我想与brink_id in employees table 和brink_id in jobs table 建立多对多关系,以brink_employee_id, brink_job_id in employee_job 表而不是ID(各个表的主列)。
但是当我尝试使用以下方法插入数据透视表时,它总是插入员工 ID 而不是 brink_id
$employee = Employee::with('jobs')->where('brink_id',$employeeJob['brink_employee_id'])->first();
$employee->jobs()->attach($employeeJob['brink_job_id'], ['is_active' => 1]);
例如如果employee表中的brink_id为123456,ID为1,则上面的employee_table中的代码将在employee_brink_id中存储1而不是123456。
请让我知道我做错了什么。
【问题讨论】:
-
你的 Laravel 版本是什么?
-
@JonasStaudenmeir 这是 5.2
-
自定义本地键仅在 Laravel 5.5+ 中支持。
标签: laravel eloquent many-to-many