PHP code example of centrex / laravel-cart

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/ */

    

centrex / laravel-cart example snippets


// config/laravel-cart.php
'driver'          => env('CART_DRIVER', 'session'),   // session | cookie | database
'default_instance'=> 'default',
'tax'             => env('CART_TAX', 0),              // integer percent, e.g. 15
'cookie_lifetime' => env('CART_COOKIE_LIFETIME', 43200),  // minutes (default 30 days)
'database' => [
    'connection' => env('CART_DB_CONNECTION'),
    'table'      => 'carts',
],

use Centrex\Cart\Facades\Cart;

// Add item (merges qty if same rowId already exists)
$item = Cart::add(
    id:      $product->id,
    name:    $product->name,
    qty:     2,
    price:   $product->price,
    options: ['color' => 'red', 'size' => 'M'],
);

echo $item->rowId;    // md5 hash of id + sorted options
echo $item->subtotal; // qty × price

// Update quantity
Cart::update($item->rowId, 5);

// Remove item
Cart::remove($item->rowId);

// Clear everything
Cart::clear();

// 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();

$item->rowId;    // string — md5(id + sorted options)
$item->id;       // string|int — your product ID
$item->name;     // string
$item->qty;      // int
$item->price;    // float — unit price
$item->options;  // array — ['color' => 'red', 'size' => 'M']
$item->subtotal; // float — qty × price
$item->toArray();

// Default cart
Cart::add(1, 'Widget', 1, 29.99);

// Wishlist cart
$wishlist = Cart::instance('wishlist');
$wishlist->add(2, 'Gadget', 1, 99.00);

Cart::count();              // 1 (default)
$wishlist->count();         // 1 (wishlist)

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
}
bash
php artisan vendor:publish --tag="laravel-cart-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-cart-config"
bash
php artisan vendor:publish --tag="laravel-cart-views"
# → resources/views/vendor/laravel-cart/livewire/cart-icon.blade.php
# → resources/views/vendor/laravel-cart/livewire/cart-drawer.blade.php