robk27 robk27 - 9 months ago 32
MySQL Question

Laravel - Inserting/Updating Pivot Table

I have a pivot table employee_project. In it, I have id, employee_id and project_id.

Each project has an Account Manager and a Project Manager. So in my form I have name (for the project) which should add the id to project_id and I also have am_id and pm_id, which refer to employee_id. How do I get these IDs into the pivot table since they are named differently than employee_id?

This does not work:

$p = Project::create($request->all());

$p->employees()->sync(['am_id', 'pm_id']);


What should I do differently?

Table Structures

--projects
id
name
stage
status
timestamps

--employees
id
name
department

--employee_project
id
employee_id
project_id

Answer Source

Naming doesnot matter.The values inside array are employee_id anyway. $p->employees()->sync([1,2]); should work. You are passing am_id and pm_id as string?

Try with:

$p->employees()->sync([$request->am_id, $request->pm_id]);