Before you mark this question as duplicate, please see the details :) I have a problem related to many to many relationship with custom columns linking
I have following tables
Employees
-Id
-brink_id <----- This is third party id I am saving of employee
-name
Jobs
-id
-brink_id <----- This is third party id I am saving of job
-description
employee_job
-id
-brink_id <----- This is third party id I am saving of relationship for third party
-brink_employee_id
-brink_job_id
In Employee Model I have created relationship
public function jobs()
{
return $this->belongsToMany('App\Models\Job','employee_job','brink_employee_id','brink_job_id');
}
And in Jobs Model
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function employees()
{
return $this->belongsToMany('App\Models\Employee','employee_job','brink_job_id','brink_employee_id');
}
Actually I want to have many to many relationship with brink_id in employees table
and brink_id in jobs table
presented as brink_employee_id, brink_job_id in employee_job
table not with ID (primary columns of respective table).
But when I try to insert in pivot table using following, it always inserts employee ID rather than brink_id
$employee = Employee::with('jobs')->where('brink_id',$employeeJob['brink_employee_id'])->first();
$employee->jobs()->attach($employeeJob['brink_job_id'], ['is_active' => 1]);
For example if brink_id in employee table is 123456 and ID is 1, the above code in employee_table will store 1 in employee_brink_id rather than 123456.
Please let me know what I am doing wrong.