Using The Cart

Using The Cart

The Cart is available via the Cart facade in the views.

Get Products in Cart

Cart::getItems() returns all products in the cart.

It returns an empty collection if the cart is empty.

Checking Whether A Cart Exists

A non-existing cart means that the current session has no cart.

Cart::exists() returns whether a cart exists for the current session.

Cart::doesNotExist() is the opposite of exists() 🤯


// false


// true

Item Count

Cart::itemCount() returns the number of items in the cart.

It also returns 0 for non-existing carts.

Is Empty Or Not?

To have a cleaner code, there are two methods to check if cart is empty:

  • Cart::isEmpty()
  • Cart::isNotEmpty()

Adding Items To The Cart

Post requests to the shop.cart.add route:

<form action="{{ route('shop.cart.add', $product) }}" method="post" class="mb-4">
    {{ csrf_field() }}

    <span class="mr-2 font-weight-bold text-primary btn-lg">
        {{ format_price($product->price) }}
    <button type="submit" class="btn btn-success btn-lg"
        @if(!$product->price) disabled @endif>
        {{ __('Add to cart') }}