Some times this error occurred due to inserting data into extra columns like ('created_at','updated_At') in pivot table. Below solution works for me.
Error Code
$users=User::create($input['data']);
foreach ($input['products'] as $product) {
if (array_key_exists('special_price', $product)) {
$userProducts[$product['id']] = ['special_price' => $product['special_price']];
} else {
$userProducts[$product['id']] = ['special_price' => ''];
}
$userProducts['created_at'] = Carbon::now()->toDateTimeString();
$userProducts['updated_at'] = Carbon::now()->toDateTimeString();
}
//individual product with special price of each product
/*
Output before code correction
array (1 =>
array(
'special_price' => '',
),
'created_at' => '2019-04-07 14:28:27',
'updated_at' => '2019-04-07 14:28:27',))
*/
$users->products()->sync($userProducts);
Code Correction
$users=User::create($input['data']);
foreach ($input['products'] as $product) {
if (array_key_exists('special_price', $product)) {
$userProducts[$product['id']] = ['special_price' => $product['special_price']];
} else {
$userProducts[$product['id']] = ['special_price' => ''];
}
// below lines are after code correction
$userProducts[$product['id']]['created_at'] = Carbon::now()->toDateTimeString();
$userProducts[$product['id']]['updated_at'] = Carbon::now()->toDateTimeString();
}
//individual product with special price of each product
/*
Output after code correction
array (1 =>
array(
'special_price' => '',
'created_at' => '2019-04-07 14:28:27',
'updated_at' => '2019-04-07 14:28:27',
),)
*/
$users->products()->sync($userProducts);