Laravel 5: syncing an extra field via pivot Laravel 5: syncing an extra field via pivot laravel laravel

Laravel 5: syncing an extra field via pivot


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:

   $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"  ]