PHPackages                             tinigin/laravel-cart - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. tinigin/laravel-cart

ActiveLibrary

tinigin/laravel-cart
====================

Laravel Cart is a customizable package for adding shopping cart functionality to Laravel applications

1.0.0(today)00MITPHPPHP ^8.2

Since Aug 14Pushed todayCompare

[ Source](https://github.com/tinigin/laravel-cart)[ Packagist](https://packagist.org/packages/tinigin/laravel-cart)[ Docs](https://github.com/tinigin/laravel-cart)[ RSS](/packages/tinigin-laravel-cart/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

Laravel Cart
============

[](#laravel-cart)

A customizable Laravel package for adding shopping cart functionality to your Laravel applications.

**Packagist:** [tinigin/laravel-cart](https://packagist.org/packages/tinigin/laravel-cart)

![PHP Version Require](https://camo.githubusercontent.com/c9f64f714c636ba27a3bba6dfd52f98426832db1262747efa54b212d16943651/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e322d626c7565)![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)![Laravel](https://camo.githubusercontent.com/3cac3e222afbf2b9096e4ff7c151a49fd4330d10c42b0ff144ca490375216d58/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d392e302532422d726564)

- [Installation](#installation)
- [Configuration](#configuration)
- [Storage Options](#storage-options)
- [Quick Start](#quick-start)
- [API Reference](#api-reference)
- [Examples](#examples)
- [Octane Support](#octane-support)
- [Contributing](#contributing)
- [License](#license)

Introduction
------------

[](#introduction)

The `Laravel Cart` is a highly customizable and lightweight package that integrates shopping cart functionality into your Laravel application. It provides a simple yet powerful API for managing cart items, with support for both **database** and **session-based** storage. Perfect for e-commerce platforms that need flexible cart management.

Features
--------

[](#features)

- **Multiple Storage Drivers**: Choose between database (persistent) and session (temporary) storage
- **Simple API**: Clean and intuitive methods for managing cart items
- **Product Extras**: Store additional product information (color, size, custom attributes, etc.)
- **Cart Metadata**: Track total, currency, promo codes, and other metadata
- **User Tracking**: Automatic user ID tracking for authenticated users
- **Cart Expiration**: Automatic cleanup of expired carts (database storage)
- **Facade Support**: Easy access via Laravel Facade pattern
- **Extensible**: Simple storage driver interface allows custom implementations
- **Octane Compatible**: Fully compatible with Laravel Octane using scoped bindings
- **Easy Integration**: Minimal configuration required, works out of the box

Installation
------------

[](#installation)

You can install the package using Composer:

```
composer require tinigin/laravel-cart
```

Run the database migrations:

```
php artisan migrate
```

Configuration
-------------

[](#configuration)

The package works with zero configuration by default (uses database storage). If you want to customize the storage type, create or update your `.env` file:

```
# Choose storage type: 'db' (default) or 'session'
CART_STORAGE=db
```

Alternatively, publish and edit the config file:

```
php artisan vendor:publish --provider="Tinigin\LaravelCart\CartServiceProvider" --tag="config"
```

This will create `config/cart.php`:

```
return [
    // Storage type: 'db' (database) or 'session'
    'storage' => env('CART_STORAGE', 'db'),

    // Database configuration
    'db' => [
        'table' => 'carts',
    ],

    // Session configuration
    'session' => [
        'key' => 'cart_data',
    ],
];
```

Storage Options
---------------

[](#storage-options)

### Database Storage (Default)

[](#database-storage-default)

Store cart data persistently in the database:

```
CART_STORAGE=db
```

**Advantages:**

- ✅ Persistent storage
- ✅ Works across different devices/browsers
- ✅ Better for authenticated users
- ✅ Automatic user tracking
- ✅ Configurable expiration

**Use case:** E-commerce sites where users may abandon and return to their cart

### Session Storage

[](#session-storage)

Store cart data in the user's session:

```
CART_STORAGE=session
```

**Advantages:**

- ✅ Fast (no database queries)
- ✅ No database overhead
- ✅ Simple implementation
- ✅ Good for quick purchases

**Use case:** Quick purchase sites, temporary shopping sessions

Quick Start
-----------

[](#quick-start)

### Using the Facade

[](#using-the-facade)

```
use Tinigin\LaravelCart\Facades\Cart;

// Add item to cart
Cart::add(productId: 1, quantity: 2);

// Add item with extra data
Cart::add(
    productId: 5,
    quantity: 1,
    extra: ['color' => 'red', 'size' => 'M']
);

// Get all items
$items = Cart::items();

// Get cart total
$total = Cart::total();

// Remove item
Cart::remove(productId: 1);

// Clear entire cart
Cart::clear();

// Get cart ID
$cartId = Cart::cartId();
```

### Using Dependency Injection

[](#using-dependency-injection)

```
use Tinigin\LaravelCart\Services\CartService;

public function addToCart(CartService $cartService)
{
    $cartService->add(productId: 10, quantity: 2);
    $items = $cartService->items();
    $total = $cartService->total();
}
```

API Reference
-------------

[](#api-reference)

### Methods

[](#methods)

MethodParametersReturnsDescription`add()``productId` (int), `quantity` (int, default: 1), `extra` (array, default: \[\])voidAdd item to cart`remove()``productId` (int)voidRemove item from cart`clear()`nonevoidClear entire cart`items()`nonearrayGet all cart items`total()`nonefloatGet cart total`cartId()`nonestringGet current cart ID`getOrCreate()`nonearrayGet or create cart### Item Structure

[](#item-structure)

```
[
    'product_id' => 1,           // Product ID
    'quantity' => 2,             // Item quantity
    'extra' => [                 // Optional: Custom data
        'color' => 'red',
        'size' => 'M',
        'name' => 'Product Name',
        'price' => 29.99,
        // ... any custom fields
    ],
]
```

Examples
--------

[](#examples)

### Example 1: Simple E-Commerce Controller

[](#example-1-simple-e-commerce-controller)

```
