Home > Back-end >  Duplicate entry in attach pivot table laravel
Duplicate entry in attach pivot table laravel

Time:01-09

I got this error

"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2-7-8' for key 'cart_product_cart_id_product_id_price_id_unique' (SQL: insert into `cart_product` (`cart_id`, `created_at`, `price_id`, `product_id`, `quantity`, `updated_at`) values (2, 2023-01-09 12:23:57, 8, 7, 1, 2023-01-09 12:23:57))"


            $cart_product = $cart->products()->where('product_id', $product->id)->where('price_id', $price->id)->first();

if ($cart_product->structure == 'single') {
            $cart->products()->attach(array([
                'product_id' => $product->id,
                'quantity'   => $request->quantity,
                'price_id'   => $price->id
            ]));

        }

How can add a row to table product_cart although it already exists

CodePudding user response:

You can do, if it exists, Update. If not, Add. (Basic concept of update or create)

if ($cart_product->structure == 'single') {
    if ($cart->products()->where('product_id', $product->id)->where('price_id', $price->id)->exists()) {
        // Update
        $cart->products()->updateExistingPivot($product->id, [
            'quantity' => $request->quantity,
            'price_id' => $price->id
        ]);
    } else {
        // Insert
        $cart->products()->attach(array([
            'product_id' => $product->id,
            'quantity'   => $request->quantity,
            'price_id'   => $price->id
        ]));
    }
}

CodePudding user response:

if you need to duplicated data cause you need to update one element

you can use sync() method like :-


$cart->products->sync(array([
                'product_id' => $product->id,
                'quantity'   => $request->quantity,
                'price_id'   => $price->id
            ]));

sync() mean if exit will update it, or will be create new record

CodePudding user response:

is one way you could modify your code to update the existing row in the cart_product table if it already exists:

$cart_product = $cart->products()->where('product_id', $product->id)->where('price_id', $price->id)->first();

if ($cart_product->structure == 'single') {
// Check if a row with the given cart_id, product_id, and price_id values already exists
$existing_row = $cart->products()->where('product_id', $product->id)->where('price_id', $price->id)->exists();

if ($existing_row) {
    // If a row with the given values already exists, update the existing row
    $cart->products()->updateExistingPivot($product->id, ['quantity' => $request->quantity]);
} else {
    // If a row with the given values does not exist, insert a new row
    $cart->products()->attach(array([
        'product_id' => $product->id,
        'quantity'   => $request->quantity,
        'price_id'   => $price->id
    ]));
}

}

This code will first check if a row with the given cart_id, product_id, and price_id values already exist in the cart_product table. If it does, the code will update the existing row with the new quantity value. If it does not exist, the code will insert a new row into the table with the given values.

I hope this helps! Let me know if you have any questions.

  • Related