Laravel 5:通过枢轴同步额外的字段
问题描述
User model:
Positions model:
On form submission I have two arrays:
Using
that syncs the position_user table as expected with the user and corresponding position ids.
However I can't work out how to populate the extra field ('company_id')
This is kind of what I would expect to work:
I have read the manual but I am just not seeing how to handle these arrays as the examples in the manual seem to relate to a situation where the extra field to be populated is not an array of multiple items:
I have tried using this answer
to combine the two arrays:
and get $syncData of :
Which maps accordingly to position id array and company id array but if I try
I get a "SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails" - I believe it is trying to add in the company_id as another position_user.position_id but then it errors out as that doesn't exist in the positions table.
Whatever I am trying at the moment my company_id
field is still not being updated/populated.
What am I doing wrong and how do I update that field?
You are actually pretty close. The required format is:
This should generate the correct array:
这篇关于Laravel 5:通过枢轴同步额外的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!