Answer the question
In order to leave comments, you need to log in
How to update records in intermediate table if data has changed?
On the checkout page - I create an order and write the data from the basket into it, write it in a loop
foreach ( $items_cart as $item ) {
//запись связанных данных в промежуточную таблицу
$order->product()->attach([ $item['item_id']=>[ 'product_quantity'=>$item['quantity'],'product_color'=>$item['color'] ] ]);
}
Answer the question
In order to leave comments, you need to log in
There are 2 ways, simple and dreary.
An easy way is to delete the old data from the trash every time and write new ones.
The tedious way is a combination of array_diff functions to find out which products to add and which to remove
$attached = array_diff($new, $old);
$deleted = array_diff($old, $new);
laravel has a great sync method. I recommend that you carefully read the documentation before asking such questions.
Search (ctrl + f) for the word sync(
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question