2

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.

Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137

1 Answers1

0

If you don't specify a different parent key and related key, it will default to your current model's primary key and the related model's primary key respectively.

Specify the parent key name as the 5th argument and the related key name as the 6th argument:

return $this->belongsToMany('App\Models\Employee', 'employee_job', 'brink_job_id', 'brink_employee_id', 'brink_id', 'brink_id');

However, I'd personally use the ids from your system and not the ids from a 3rd party system in your relationships.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • Changed as asked but still not saving employee_brink_id. It is still saving its ID. But it is actually saving brink job id. `return $this->belongsToMany('App\Models\Job','employee_job','brink_employee_id','brink_job_id', 'brink_id', 'brink_id');` – Awais Qarni Sep 04 '18 at 13:15
  • so brink_job_id matches but not brink_employee_id? How are you syncing records? – Devon Bessemer Sep 04 '18 at 13:19
  • I am actually using `attach`. Find employee `$employee = Employee::with('jobs')->where('brink_id',$employeeJob['brink_employee_id'])->first();` And then attachig as $employee->jobs()->attach($employeeJob['brink_job_id'], ['is_active' => 1]); – Awais Qarni Sep 04 '18 at 13:21
  • Don't attach using the brink_job_id, attach the model instance or use the model's primary key. – Devon Bessemer Sep 04 '18 at 13:24
  • didn't get `Don't attach using the job id, attach the model instance`. Can you please explain a little bit more as job id is actually saving correctly. But not the employee ID – Awais Qarni Sep 04 '18 at 13:26
  • attach expects the primary key of that model, not the foreign key. Since you already have a model instance in $employeeJob, just use that. – Devon Bessemer Sep 04 '18 at 14:45