I want to update pivot table in laravel 8 .I'm trynig sync method, but only insert first item of an array.
Here is my code
foreach ($products as $product) {
$quantity = $product['quantity'];
$product_id = $product['product_id'];
$food_item_price = FoodItemPrice::where('food_item_id', $product_id)->first();
$order->orderFoodItems()->sync([$product_id => [
'quantity' => $quantity,
'food_item_price_id' => $food_item_price->id,
]]);
}
In $products may have many products, but with sync method, it only updates first product. How to update pivot table properly?