简体   繁体   English

Laravel 5:通过枢轴同步一个额外的字段

[英]Laravel 5: syncing an extra field via pivot

User model:用户模型:

public function positions()
{
 return $this->belongsToMany('App\Position')->withPivot('company_id')->withTimestamps();

}

Positions model:职位模型:

public function users()
{
 return $this->belongsToMany('App\User')->withPivot('company_id')->withTimestamps();
}

On form submission I have two arrays:在提交表单时,我有两个数组:

$allPositionIds
array:3 [
0 => 98
1 => 99
2 => 100
]


$allCompanyIds
array:3 [
0 => 129
1 => 130
2 => 131
]

Using使用

$user->positions()->sync($allPositionIds);

that syncs the position_user table as expected with the user and corresponding position ids.按预期将 position_user 表与用户和相应的位置 ID 同步。

However I can't work out how to populate the extra field ('company_id')但是我不知道如何填充额外的字段('company_id')

This is kind of what I would expect to work:这就是我期望的工作:

$user->positions()->sync([$allPositionIds => ['company_id' => $allCompanyIds]], false);

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:我已经阅读了手册,但我只是没有看到如何处理这些数组,因为手册中的示例似乎与要填充的额外字段不是多个项目的数组的情况有关:

$user->roles()->sync(array(1 => array('expires' => true)));

I have tried using this answer我试过使用这个答案

to combine the two arrays:组合两个数组:

$syncData = array_combine($allPositionIds,$allCompanyIds);

and get $syncData of :并获得 $syncData :

array:3 [
98 => 129
99 => 130
100 => 131
]

Which maps accordingly to position id array and company id array but if I try哪个映射到位置 id 数组和公司 id 数组,但如果我尝试

user->positions()->sync($syncData);

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.我收到一个"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.无论我现在尝试什么,我的company_id字段仍未更新/填充。

What am I doing wrong and how do I update that field?我做错了什么,如何更新该字段?

You are actually pretty close.你实际上很接近。 The required format is:所需格式为:

[
    98 => ['company_id' => 129],
    99 => ['company_id' => 130],
    100 => ['company_id' => 131]
]

This should generate the correct array:这应该生成正确的数组:

$extra = array_map(function($companyId){
    return ['company_id' => $companyId];
}, $allCompanyIds);

$data = array_combine($allPositionIds, $extra);

$user->positions()->sync($data);

Based on the answer of @lukasgeiter, here there is an example to combine two extra field for a pivot table:根据@lukasgeiter 的回答,这里有一个示例,可以为数据透视表合并两个额外字段:

   $extra = array_map(function($qualityId) use($request){
                return ['quality_id' => $qualityId, 'product_id' => $request->product];
            }, $arrayQualitiesIds);
            
            $data = array_combine($arrayQualitiesIds, $extra);
            dd($data);

Output:输出:

1 => array:2 [▼
    "quality_id" => "1"
    "product_id" => "5"
  ]
  2 => array:2 [▼
    "quality_id" => "2"
    "product_id" => "5"
  ]
  3 => array:2 [▼
    "quality_id" => "3"
    "product_id" => "5"
  ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM