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..