'Add and update products to session cart in Laravel

I am building a shopping cart for guests with sessions in laravel. I can add one product with quantity to the session variable 'cart.' However, when I add the same product again, I cannot increment quantity as I cannot search for the same product ID in the array.

Controller

public function addToSessionCart(Request $request, $id)
{
    if (session()->has('cart')) {
        $oldCart = session()->get('cart');
        $cartdata = $oldCart;
        if (array_key_exists('product_id', $cartdata)) {
            if (in_array($id, $cartdata['product_id'], true)) {
                $cartdata['quantity'] += 1;
            } else {
                $cartdata['product_id'] = $id;
                $cartdata['quantity'] = 1;
            }
        }
    } else {
        $cartdata = [];
        $cartdata['product_id'] = $id;
        $cartdata['quantity'] = 1;
    }
    session()->put('cart', $cartdata);
    session()->flash('message', 'Item added to cart !');

    return back();
}

I am passing data to the cart from my AppServiceProvider:

view()->composer('*', function ($view) {
    if (Auth::check()) {
        $cart_items = Cart::where('user_id', Auth::user()->id)->with('product')->get();
        $cart_items_count = Cart::where('user_id', Auth::user()->id)->count();
        $menus = Menu::where('status', '1')->orderBy('order', 'ASC')->get();
        $view->with(['cart_items' => $cart_items, 'cart_items_count' => $cart_items_count, 'menus' => $menus]);
    } else {
        $cart_items = null;
        $session_cart_items = null;
        if (\Session::get('cart')) {
            $session_cart_items = [];
            foreach (session('cart') as $session_cart_items[]) {
                $session_cart_items['product'] = Product::where('id', session('cart.product_id'))->get();
                $session_cart_items['quantity'] = session('cart.quantity');
            }
        }

        $cart_items_count = count(\Session::get('cart'));
        $menus = Menu::where('status', '1')->orderBy('order', 'ASC')->get();
        $view->with([
            'cart_items' => $cart_items, 'cart_items_count' => $cart_items_count, 'menus' => $menus,
            'session_cart_items' => $session_cart_items,
        ]);
    }
}); 

View

@foreach($session_cart_items['product'] as $key => $name)
    <div class="product-cart">
        <div class="product-image">
            <img src="/frontend/assets/images/{{ $name->image }}" alt="image">
        </div>
        <div class="product-content">
            <h3><a href="/product/{{$name->id}}/{{$name->slug}}">{{$name->name}}</a></h3>
            <div class="product-price">
                <span>{{ $session_cart_items['quantity'] }}</span>
                <span>x</span>
                &#8377; <span class="price">{{ $name->price }}</span>
            </div>
        </div>
    </div>
    @php
        $total += ($name->price * $session_cart_items['quantity']);
    @endphp
@endforeach

I want the quantity to be incremented when the same product gets added to the cart. If not, add a new product id and quantity to the session array.



Solution 1:[1]

This line needs to be changed:

foreach (session('cart') as $session_cart_items[]) {

You cannot use [] for reading.

foreach (session('cart') as $session_cart_items) {

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1