1. Go to this page and download the library: Download centrex/laravel-cart library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// All items — Collection<string, CartItem>
Cart::content();
// Single item (throws CartItemNotFoundException if missing)
Cart::get($rowId);
// Aggregates
Cart::count(); // total units (sum of all qty)
Cart::lines(); // number of distinct line items
Cart::subtotal(); // sum of all item subtotals
Cart::tax(); // subtotal × (tax% / 100)
Cart::total(); // subtotal + tax
Cart::isEmpty();
use Centrex\Cart\Events\CartItemAdded;
use Centrex\Cart\Events\CartItemUpdated;
use Centrex\Cart\Events\CartItemRemoved;
use Centrex\Cart\Events\CartCleared;
Event::listen(CartItemAdded::class, function (CartItemAdded $event) {
// $event->item — CartItem
// $event->instance — 'default'
});
// In your LoginController / AuthenticatedSessionController
use Centrex\Cart\Models\StoredCart;
StoredCart::where('identifier', session()->getId())
->update(['identifier' => (string) auth()->id()]);
// In your ProductCard or similar Livewire component
public function addToCart(int $productId): void
{
$product = Product::findOrFail($productId);
Cart::add($product->id, $product->name, 1, $product->price);
$this->dispatch('cart-updated'); // refreshes CartIcon badge + CartDrawer
$this->dispatch('open-cart'); // optionally open the drawer
}