Saturday, March 11, 2017

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?



via robk27

Advertisement