Laravel 5: syncing an extra field via pivot
Asked Answered
L

2

8

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.

However I can't work out how to populate the extra field ('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 :

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

Which maps accordingly to position id array and company id array but if I try

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.

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?

Lector answered 13/5, 2015 at 17:51 Comment(2)
Remove foreign key checks and try again and see what happens with the company_id, it might give you a better idea of what's happening.Cristiecristin
Hey past me, thanks for absolutely nothing. You created the most insanely terrible situation that I now have to debug and completely rewrite while factoring in live data. What a noob. Sheez.Lector
S
15

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);
Sensuality answered 13/5, 2015 at 19:37 Comment(1)
Yes! Fantastic, thanks@lukasgeiter. Awesome! Going to bookmark this and when I have enough of a rep I am going to come back and upvote !Lector
E
0

Based on the answer of @lukasgeiter, here there is an example to combine two extra field for a pivot table:

   $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"
  ]
Excitable answered 25/8, 2021 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.