I want to update the order products by pivot ID using sync method because in my scenario an order can have multiple products with the same ID and I want to update the product whose pivot ID I am calling and delete the other ones which I have not Updated product, but you know from the sync syntax that it accepts the product's id.
Example
This is what my body and pivot table look like when I attach a product to an order
{ "products": [ { "product_id": 1, "color": "red", "quantity": 2 }, { "product_id": 1, "color": "black", "quantity": 10 }, { "product_id": 2, "color": "black", "quantity": 1 } ] }
id | order_id | Product ID | color | quantity |
---|---|---|---|---|
1 | 1 | 1 | red | 2 |
2 | 1 | 1 | black | 10 |
3 | 1 | 2 | black | 1 |
When updating order products
{ "products": [ { "id" : 1, "product_id" :1 , "color": "blue", "quantity": 12 }, { "id" : 3, "product_id" :2, "color": "blue", "quantity": 5 } ]}
What do I want my table to look like
id | order_id | Product ID | color | quantity |
---|---|---|---|---|
1 | 1 | 1 | blue | 12 |
3 | 1 | 2 | blue | 5 |
But got this expected error
"message": "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY' (SQL: insert into `order_product` (`color`, `created_at`, `id`, `order_id`, `product_id`, `quantity`, `size`, `updated_at`) values (blue, 2022-07-04 21:38:25, 7, 3, 1, 12, S, 2022-07-04 21:38:25))"
Order Controller
public function update(AdminUpdateOrderRequest $request, $id) { $orderValidated = $request->validated(); $order = Order::findOrFail($id); $order->update($orderValidated); if (isset($orderValidated['products'])) { $order->products()->sync($orderValidated['products']); } DB::table return OrderResource::make($order)->additional([ 'success' => true, ]); }
Order product migration
Schema::create('order_product', function (Blueprint $table) { $table->id(); $table->foreignId('order_id')->nullable()->constrained('orders')->onUpdate('cascade'); $table->foreignId('product_id')->nullable()->constrained('products')->onUpdate('cascade'); $table->integer('quantity')->nullable(); $table->string('color')->nullable(); $table->timestamps(); });
Any ideas how to solve my problem?
You need to handle this without belonging to multiple relationships and have to create an intermediate or pivot model.
And you need to change the order and product model (if not used, the product model is optional)
In your controller update method
// Assume your request structure is
You need to create your own sync method
I hope this helps and you may want to use transactions to prevent errors and add corresponding validation