How to add every item as a new record without updating in laravel in cart

  laravel, php

I have a question regarding Laravel and I am new to Laravel.

I am creating a website with has multiple products and each product can be customized. Every time I add the same product to the cart, it overwrites the previous record which has the same product id(as expected). In my case I want the new item in cart, every-time a user add product to their cart even if they add same item again. How can i achieve this?

    if(request()) {              
    $cart = session()->get('cart');
    $cart[request()->product_id][(count(session()->get('cart')))] = [
    "name" => request()->name,
    "tel" => request()->tel,
    "email" => request()->email,
    "linkedin" => request()->linkedin,
    "slogan" => request()->slogan,
    "logo" => request()->logo,
    "qty" => request()->qty,
    "product_id" => request()->product_id,
]; session()->put('cart', $cart);

I am not able to achieve, my record overwrites if the product_id is the same.

Source: Laravel

Leave a Reply