Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bastet's avatar

Undefined variable

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
0 likes
12 replies
munazzil's avatar

According to your controller code you getting a collection then you have to use foreach function for get display every data as like below,

  <div class="content_wrapper">
         @foreach($shoppingCart as $shops)
            {{ $shops }}
         @endforeach
  </div>

in case you have used first() instead of get()as like below you will get only one data then you can use whatever you have given blade code,

  $shoppingCart = ShoppingCart::where('user_id', Auth::user()->id)->first();   

  <div class="content_wrapper">
    {{ $shoppingCart }}
  </div>
MiguelBarros's avatar

if(count($shoppingCart) > 0)
        {
            $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 $updateShoppingCart)
            {
                $updateShoppingCart->cart = json_encode($cart);
                $updateShoppingCart->save();
            }
        }

MaverickChan's avatar
$shoppingCart = ShoppingCart::where('user_id', Auth::user()->id)->get();
//spell wrong??

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

also , this line should be inside auth check .

if you are not authenticated , you will never get the variable right.

manelgavalda's avatar

You are having this error on your controller or in your blade? The code seems right so this may be a problem with cache or compilation. Try to run this commands:

php artisan cache:clear

composer du
Bastet's avatar

Sorry for only getting back to everybody now. But I've tried the solutions and I'm still getting the undefined variable

ajithlal's avatar
return view('public.shopping-cart', compact('shoppingCart'));

try this

ajithlal's avatar
public function getCart()
    {
        $shoppingCart = '';
        $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
        ]);

    }

Check this code. I think you are not getting a value from the first line. I mean

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

is not returning anything. Please check with this.

cujo's avatar

I think you don't know what kind of data you want. At the beginning you're defining $shoppingCart as collection, but later on $newShoppingCart is single model. Also in the else statement $shoppingCart is used in foreach, and that means it became single model (but previously was collection).

public function getCart()
{
    // It should be under first if. Otherwise Auth::user()->id() will throw an error is user is not authenticated if 
    // you don't have some kind of guest user model defined.
    $shoppingCart = ShoppingCart::where('user_id', Auth::user()->id())->get();

    if(Auth::check())
    {
        if($shoppingCart->isEmpty())
        {
        // What is that? Shouldn't you add it to collection and pass that to blade?
            $newShoppingCart = new ShoppingCart();
            $newShoppingCart->user_id = Auth::user()->id;
            // What is $cart variable?
            $newShoppingCart->cart = json_encode($cart);
            $newShoppingCart->save();
        }else{
            $user_id = Auth::user()->id;
            $updateShoppingCart = ShoppingCart::where('user_id', $user_id)->get();

            // Avoid using same name for variables that should contain different objects.
            // Here $shoppingCart is supposed to be Eloquent model
            // but above it's meant to be a collection.
            foreach($updateShoppingCart as $shoppingCart)
            {
                // Again, what is $cart variable?
                $shoppingCart->cart = json_encode($cart);
                $shoppingCart->save();
            }
        }
    }

    // Here you are expecting that $shoppingCart is an collection or single model?
    return view('public.shopping-cart', [
        'shoppingCart' => $shoppingCart
    ]);
}
@extends('layouts.public')
@section('content')
    <div class="content_wrapper">
        <!-- Same here, are you want to handle model or collection? -->
        {{ $shoppingCart }}
    </div>
@stop

Describing context and what you want to achieve would be helpful.

// Edit: Also, if you're serving your app by CLI command (php artisan serve) have you refreshed it?

Please or to participate in this conversation.