There is data get from a User, and I want to sync them based on the code
:
the cart_variation
table has these columns:
cart_id, variation_id, code, title, type
I want to sync without detaching data based on the code
. if code
exists update other data like type, title else insert. I mean among data as :
here is some code but don't work properly:
$this->instance()->variations()->syncWithoutDetaching([$variation->id =>
[$code => ['title' => $title, 'type' => $type]],
],
OR
this code don't sync code and I make many entries with same code
DB::table('cart_variation')
->updateOrInsert(
['cart_id' => $this->cart->id, 'variation_id' => $variation->id, 'code' => $code],
['title' => $title, 'type' => $type]
);
CodePudding user response:
DB::table('cart_variation')
->updateOrCreate(
['code' => $code],
['title' => $title, 'type' => $type, 'cart_id' => $this->cart->id, 'variation_id' => $variation->id]
);
For updateOrCreate
the first array is the comparison array.
So basically the above code will check if the $code
you are providing exists:
- if the case is yes it will update the fields
title,type,cart_id,variation_id
- otherwise will create a new record