PHPackages                             foridom/laravel-shopping-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. foridom/laravel-shopping-cart

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

foridom/laravel-shopping-cart
=============================

Shopping cart for Laravel Application.

1.3.4(6y ago)07MITPHPPHP &gt;=5.6

Since Jan 18Pushed 6y agoCompare

[ Source](https://github.com/foridom/laravel-shopping-cart)[ Packagist](https://packagist.org/packages/foridom/laravel-shopping-cart)[ RSS](/packages/foridom-laravel-shopping-cart/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (4)Versions (9)Used By (0)

[![Build Status](https://camo.githubusercontent.com/27bc7d348a6ef4860af4816ffcd85bde602d8afae069b63765552bc8d92363bf/68747470733a2f2f7472617669732d63692e6f72672f67756f6a69616e67636c75622f6c61726176656c2d73686f7070696e672d636172742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ibrandcc/laravel-shopping-cart)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ee4e8800ec2e533b4c8d3fcbba0d4b59a02f9ea8b49f10a00ff2b4c9dd43ab9c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f67756f6a69616e67636c75622f6c61726176656c2d73686f7070696e672d636172742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/guojiangclub/laravel-shopping-cart/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/aba5971fe06806bf7160e0dd24441b321cbf23d8ed72052ed68302edd7dbfb77/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f67756f6a69616e67636c75622f6c61726176656c2d73686f7070696e672d636172742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/guojiangclub/laravel-shopping-cart/?branch=master)[![Build Status](https://camo.githubusercontent.com/bc438013ee95c593769dc42a39ca25c4497dfcceb2943ae024878f35d63fd589/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f67756f6a69616e67636c75622f6c61726176656c2d73686f7070696e672d636172742f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/guojiangclub/laravel-shopping-cart/build-status/master)[![Latest Stable Version](https://camo.githubusercontent.com/9d74bf3baf6ffd06d2cce323ad457e4ed3dd546dd0821b51247317a317076123/68747470733a2f2f706f7365722e707567782e6f72672f696272616e642f6c61726176656c2d73686f7070696e672d636172742f762f737461626c65)](https://packagist.org/packages/ibrand/laravel-shopping-cart)[![Latest Unstable Version](https://camo.githubusercontent.com/7211370fc131772f5799952119e4f38102196c624d7f6d7240b8104989ec3e69/68747470733a2f2f706f7365722e707567782e6f72672f696272616e642f6c61726176656c2d73686f7070696e672d636172742f762f756e737461626c65)](https://packagist.org/packages/ibrand/laravel-shopping-cart)[![License](https://camo.githubusercontent.com/db7f45a81cb11983723c865aa5b29025e24293c5cde14af4b0fdbe5bfc652e04/68747470733a2f2f706f7365722e707567782e6f72672f696272616e642f6c61726176656c2d73686f7070696e672d636172742f6c6963656e7365)](https://packagist.org/packages/ibrand/laravel-shopping-cart)

购物车在电商场景中基本是必须的一个模块，本包是基于 [overtrue/laravel-shopping-cart](https://github.com/overtrue/laravel-shopping-cart) 进行扩展开发，主要实现了以下扩展：

1. 购物车数据支持 Database 存储
2. Item 增加 Model 属性返回。因为购物车可能是SPU或者SKU，因此直接通过 model 属性直接返回相关对象。
3. 支持多 Guard. 因为在果酱小店中有商城购物车和导购购物车。

> 已经完成了 Session 和 Database 模式下的单元测试，而且正在果酱小店产品线上使用中. 可放心使用.

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

[](#installation)

```
composer require ibrand/laravel-shopping-cart:~1.0 -vvv

```

```
php artisan vendor:publish --provider="iBrand\Shoppingcart\ServiceProvider"

```

低于 Laravel5.5 版本

`config/app.php` 文件中 'providers' 添加

```
iBrand\Shoppingcart\ServiceProvider::class

```

`config/app.php` 文件中 'aliases' 添加

```
'Cart'=> iBrand\Shoppingcart\Facade::class

```

Usage
-----

[](#usage)

### Select Storage

[](#select-storage)

You can change data Storage in `config/ibrand/cart.php` file.

```
'storage' => \iBrand\Shoppingcart\Storage\DatabaseStorage::class,

'storage' => \iBrand\Shoppingcart\Storage\SessionStorage::class,
```

If you use Database Storage, you need to execute `php artisan migrate`

### Add item to cart

[](#add-item-to-cart)

Add a new item.

```
Item | null Cart::add(
                    string | int $id,
                    string $name,
                    int $quantity,
                    int | float $price
                    [, array $attributes = []]
                 );
```

**example:**

```
$row = Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
// Item:
//    id       => 37
//    name     => 'Item name'
//    qty      => 5
//    price    => 100.00
//    color    => 'red'
//    size     => 'M'
//    total    => 500.00
//    __raw_id => '8a48aa7c8e5202841ddaf767bb4d10da'
$rawId = $row->rawId();// get __raw_id
$row->qty; // 5
...
```

### Update item

[](#update-item)

Update the specified item.

```
Item Cart::update(string $rawId, int $quantity);
Item Cart::update(string $rawId, array $arrtibutes);
```

**example:**

```
Cart::update('8a48aa7c8e5202841ddaf767bb4d10da', ['name' => 'New item name');
// or only update quantity
Cart::update('8a48aa7c8e5202841ddaf767bb4d10da', 5);
```

### Get all items

[](#get-all-items)

Get all the items.

```
Collection Cart::all();
```

**example:**

```
$items = Cart::all();
```

### Get item

[](#get-item)

Get the specified item.

```
Item Cart::get(string $rawId);
```

**example:**

```
$item = Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');
```

### Remove item

[](#remove-item)

Remove the specified item by raw ID.

```
boolean Cart::remove(string $rawId);
```

**example:**

```
Cart::remove('8a48aa7c8e5202841ddaf767bb4d10da');
```

### Destroy cart

[](#destroy-cart)

Clean Shopping Cart.

```
boolean Cart::destroy();
boolean Cart::clean(); // alias of destroy();
```

**example:**

```
Cart::destroy();// or Cart::clean();
```

### Total price

[](#total-price)

Returns the total of all items.

```
int | float Cart::total(); // alias of totalPrice();
int | float Cart::totalPrice();
```

**example:**

```
$total = Cart::total();
// or
$total = Cart::totalPrice();
```

### Count rows

[](#count-rows)

Return the number of rows.

```
int Cart::countRows();
```

**example:**

```
Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(127, 'foobar', 15, 100.00, ['color' => 'green', 'size' => 'S']);
$rows = Cart::countRows(); // 2
```

### Count quantity

[](#count-quantity)

Returns the quantity of all items

```
int Cart::count($totalItems = true);
```

`$totalItems` : When `false`,will return the number of rows.

**example:**

```
Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
$count = Cart::count(); // 11 (5+1+5)
```

### Search items

[](#search-items)

Search items by property.

```
Collection Cart::search(array $conditions);
```

**example:**

```
$items = Cart::search(['color' => 'red']);
$items = Cart::search(['name' => 'Item name']);
$items = Cart::search(['qty' => 10]);
```

### Check empty

[](#check-empty)

```
bool Cart::isEmpty();
```

### Specifies the associated model

[](#specifies-the-associated-model)

Specifies the associated model of item.

```
Cart Cart::associate(string $modelName);
```

**example:**

```
Cart::associate('App\Models\Product');
$item = Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->product->name; // $item->product is instanceof 'App\Models\Product'
```

The Collection And Item
=======================

[](#the-collection-and-item)

`Collection` and `Overtrue\LaravelShoppingCart\Item` are instanceof `Illuminate\Support\Collection`, Usage Refer to：[Collections - Laravel doc.](http://laravel.com/docs/5.0/collections)

properties of `Overtrue\LaravelShoppingCart\Item`:

- `id` - your goods item ID.
- `name` - Name of item.
- `qty` - Quantity of item.
- `price` - Unit price of item.
- `total` - Total price of item.
- `__raw_id` - Unique ID of row.
- `__model` - Name of item associated Model.
- ... custom attributes.

And methods:

- `rawId()` - Return the raw ID of item.

Events
======

[](#events)

Event NameParameters`cart.adding`($attributes, $cart);`cart.added`($attributes, $cart);`cart.updating`($row, $cart);`cart.updated`($row, $cart);`cart.removing`($row, $cart);`cart.removed`($row, $cart);`cart.destroying`($cart);`cart.destroyed`($cart);You can easily handle these events, for example:

```
Event::on('cart.adding', function($attributes, $cart){
    // code
});
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 91.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~113 days

Recently: every ~182 days

Total

8

Last Release

2239d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/476417bab57fef70776c0685e56acbda4a3c1039e10643531b982f0de737d19a?d=identicon)[foridom](/maintainers/foridom)

---

Top Contributors

[![chenbidepro](https://avatars.githubusercontent.com/u/9166101?v=4)](https://github.com/chenbidepro "chenbidepro (34 commits)")[![tangqi1916](https://avatars.githubusercontent.com/u/193469035?v=4)](https://github.com/tangqi1916 "tangqi1916 (2 commits)")[![fans1992](https://avatars.githubusercontent.com/u/26118553?v=4)](https://github.com/fans1992 "fans1992 (1 commits)")

---

Tags

laravelshoppingshopping cart

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/foridom-laravel-shopping-cart/health.svg)

```
[![Health](https://phpackages.com/badges/foridom-laravel-shopping-cart/health.svg)](https://phpackages.com/packages/foridom-laravel-shopping-cart)
```

###  Alternatives

[ibrand/laravel-shopping-cart

Shopping cart for Laravel Application.

385.6k1](/packages/ibrand-laravel-shopping-cart)[syscover/shopping-cart

Shopping Cart package

299.1k1](/packages/syscover-shopping-cart)[wearepixel/laravel-cart

A cart implementation for Laravel

1310.5k](/packages/wearepixel-laravel-cart)[ultrono/laravelshoppingcart-1

Laravel 11 and above Shopping cart

1110.0k](/packages/ultrono-laravelshoppingcart-1)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
