Bastet
3 months ago
26
12
Laravel

Undefined variable

Posted 3 months ago by Bastet

This might be silly, but I don't know what I'm missing, but I keep getting this error

Undefined variable: shoppingCart

and as far as I know, I'm passing the variable across.

here is my function in the controller

public function getCart()
{

    $shoppingCart = ShoppingCart::where('user_id', Auth::user()->id)->get();
    
    if(Auth::check())
    {

        if($shoppingCart->isEmpty())
        {
            $newShoppingCart = new ShoppingCart();
            $newShoppingCart->user_id = Auth::user()->id;
            $newShoppingCart->cart = json_encode($cart);
            $newShoppingCart->save();
        }else{
            $user_id = Auth::user()->id;
            $updateShoppingCart = ShoppingCart::where('user_id', $user_id)->get();

            foreach($updateShoppingCart as $shoppingCart)
            {
                $shoppingCart->cart = json_encode($cart);
                $shoppingCart->save();
            }
        }
    }



    return view('public.shopping-cart', [
                                        'shoppingCart' => $shoppingCart
                                    ]);

}

and this is the view

@extends('layouts.public')
@section('content')
    <div class="content_wrapper">
        {{ $shoppingCart }}
    </div>
@stop

Please sign in or create an account to participate in this conversation.