2
votes

I'm trying to set up this shopping cart library in Laravel, but I keep getting Non-static method Darryldecode\Cart\Cart::add() should not be called statically, assuming $this from incompatible context https://github.com/darryldecode/laravelshoppingcart

here's my controller code:

public function getCart(){
    $cartCollection = Cart::getContent(); 
    return response()->json($cartCollection->toArray());
}


public function updateCart( Request $request, $id ){
    $input = $request->all(); 
    Cart::update($id, array_except($input, array('_token')));
}   

public function addToCart( Request $request ){
    $input= $request->all(); 
    Cart::add(array_except($input, array('_token')));
}

public function removeFromCart( $id ){
    Cart::remove($id);
}

my confusion is that the documentation for this library says that you should be calling the methods statically. I also tried using the dependency ejection method, and that threw a different error. Any help with this would be greatly appreciated.

I'm pretty new to Laravel so forgive me if this is a dumb oversight.

also I fallowed all the steps to set up this library, including adding it to the App.php providers array and the aliases array.

1

1 Answers

10
votes

Instead of calling the static methods on the Darryldecode\Cart\Cart class you should do that on the facade of the packages. In your case that probably just means removing a use Darryldecode\Cart\Cart; statement in your controller.

Because the controller is probably inside a namespace of it's own you now have to either reference it by \Cart or add use Cart to make sure the alias is used for the calls.

Take a look at the documentation to learn more about Laravel Facades and how they work