class ProjectEmployees extends Migration
{
public function up()
{
// employees assigned to each project
Schema::create('project_employees', function (Blueprint $table) {
$table->integer("project_id")->unsigned();
$table->integer("employee_id")->unsigned();
$table->tinyInteger("is_project_leader")->unsigned()->default(0);
$table->tinyInteger("is_manager")->unsigned()->default(0);
});
Schema::table('project_employees', function($table) {
$table->primary(['project_id', 'employee_id']);
// FK
$table->foreign('employee_id')->references('id')->on('employees');
$table->foreign('project_id')->references('id')->on('projects');
});
}
}
I have a model for projects and another for employees. I want to be able to assign employees as project leaders and project managers. I have this method:
function assignProjectLeaders($project, $employees)
{
$syncData = $this->addPivotValue($employees, ['is_project_leader' => true]);
$project->projectLeaders()->sync($syncData);
}
function addPivotValue($items, $pivotValues): array
{
$pivotData = array_fill(0, count($items), $pivotValues);
$syncData = array_combine($items, $pivotData);
return $syncData;
}
So I want the method assignProjectLeaders to set the given employees as Project Leaders of the given project object. However if an employee is already associated to the project as project manager this doesnt work because of the primary key restriction (it seems to be trying to insert again the pair (project, employee) instead of just updating the is_project_leader column to 1).
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire