在php / laravel中定义类中的数组

I have a class called shoppingCart and i would like to define an array inside of it. But it seems i need to manually create an instance of an array to use it. I am sure there is a way but i can`t find it My shopping chart class

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Product;

class shoppingChart extends Model
{
    var $products = Array();
    var $productCount=0;

    public function ItemCount(){
       return count($products);
    }
}

When i try to use i get a null pointer exceptionn and whenn i checked it $products doesn`t seem to be in variable list.

 array_push($sc->products, "test");

I can do it as below. When i am using the array. Works fine.

public function addToBasket(Request $request)
{   
    $product = new Product();
    $product->id = Input::get('product_id');


    if($request->session()->has('shoppingCart')){
        $sc = $request->session()->get('shoppingCart');
        $sc->products = Array(); // If i remove this line code doesn`t work
        array_push($sc->products,$product);
        $sc->productCount=$sc->itemCount();
    }
}

Should i initiate the array everytime i use it ? Doesn`t make any sense to me..

Without knowing more about the architecture of your application i can't go into specifics but I have a feeling that you might not even need this array.

If you are looking at a ShoppingCart has many Product's, then try using an eloquent "Many to Many" relationship using relationships \Illuminate\Database\Eloquent\Relations\HasMany.

https://laravel.com/docs/5.4/eloquent-relationships#many-to-many

By having this relationship you will be able to get all products related to you cart by going $cart->products which will return an eloquent Collection of the products associated with the cart.