Laravel 5: 通过 pivot 同步一个额外的字段
Laravel 5: syncing an extra field via pivot
用户模型:
public function positions()
{
return $this->belongsToMany('App\Position')->withPivot('company_id')->withTimestamps();
}
职位模型:
public function users()
{
return $this->belongsToMany('App\User')->withPivot('company_id')->withTimestamps();
}
在提交表单时我有两个数组:
$allPositionIds
array:3 [
0 => 98
1 => 99
2 => 100
]
$allCompanyIds
array:3 [
0 => 129
1 => 130
2 => 131
]
使用
$user->positions()->sync($allPositionIds);
这会按预期将 position_user table 与用户和相应的位置 ID 同步。
但是我不知道如何填充额外字段 ('company_id')
这是我期望的工作方式:
$user->positions()->sync([$allPositionIds => ['company_id' => $allCompanyIds]], false);
我已阅读 manual 但我只是不知道如何处理这些数组,因为手册中的示例似乎与要填充的额外字段不是多个数组的情况有关项目:
$user->roles()->sync(array(1 => array('expires' => true)));
我试过用这个answer
合并两个数组:
$syncData = array_combine($allPositionIds,$allCompanyIds);
并获取 :
的 $syncData
array:3 [
98 => 129
99 => 130
100 => 131
]
相应地映射到位置 ID 数组和公司 ID 数组,但如果我尝试
user->positions()->sync($syncData);
我得到一个"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.
无论我现在正在尝试什么,我的 company_id
字段仍然不是 updated/populated。
我哪里做错了,我该如何更新该字段?
你其实很接近。要求的格式是:
[
98 => ['company_id' => 129],
99 => ['company_id' => 130],
100 => ['company_id' => 131]
]
这应该生成正确的数组:
$extra = array_map(function($companyId){
return ['company_id' => $companyId];
}, $allCompanyIds);
$data = array_combine($allPositionIds, $extra);
$user->positions()->sync($data);
根据@lukasgeiter 的回答,这里有一个将两个额外字段组合为一个主元的示例 table:
$extra = array_map(function($qualityId) use($request){
return ['quality_id' => $qualityId, 'product_id' => $request->product];
}, $arrayQualitiesIds);
$data = array_combine($arrayQualitiesIds, $extra);
dd($data);
输出:
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"
]
用户模型:
public function positions()
{
return $this->belongsToMany('App\Position')->withPivot('company_id')->withTimestamps();
}
职位模型:
public function users()
{
return $this->belongsToMany('App\User')->withPivot('company_id')->withTimestamps();
}
在提交表单时我有两个数组:
$allPositionIds
array:3 [
0 => 98
1 => 99
2 => 100
]
$allCompanyIds
array:3 [
0 => 129
1 => 130
2 => 131
]
使用
$user->positions()->sync($allPositionIds);
这会按预期将 position_user table 与用户和相应的位置 ID 同步。
但是我不知道如何填充额外字段 ('company_id')
这是我期望的工作方式:
$user->positions()->sync([$allPositionIds => ['company_id' => $allCompanyIds]], false);
我已阅读 manual 但我只是不知道如何处理这些数组,因为手册中的示例似乎与要填充的额外字段不是多个数组的情况有关项目:
$user->roles()->sync(array(1 => array('expires' => true)));
我试过用这个answer
合并两个数组:
$syncData = array_combine($allPositionIds,$allCompanyIds);
并获取 :
的 $syncDataarray:3 [
98 => 129
99 => 130
100 => 131
]
相应地映射到位置 ID 数组和公司 ID 数组,但如果我尝试
user->positions()->sync($syncData);
我得到一个"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.
无论我现在正在尝试什么,我的 company_id
字段仍然不是 updated/populated。
我哪里做错了,我该如何更新该字段?
你其实很接近。要求的格式是:
[
98 => ['company_id' => 129],
99 => ['company_id' => 130],
100 => ['company_id' => 131]
]
这应该生成正确的数组:
$extra = array_map(function($companyId){
return ['company_id' => $companyId];
}, $allCompanyIds);
$data = array_combine($allPositionIds, $extra);
$user->positions()->sync($data);
根据@lukasgeiter 的回答,这里有一个将两个额外字段组合为一个主元的示例 table:
$extra = array_map(function($qualityId) use($request){
return ['quality_id' => $qualityId, 'product_id' => $request->product];
}, $arrayQualitiesIds);
$data = array_combine($arrayQualitiesIds, $extra);
dd($data);
输出:
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"
]