PHPackages                             overtrue/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. overtrue/laravel-shopping-cart

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

overtrue/laravel-shopping-cart
==============================

Shopping cart for Laravel Application.

2.0.2(6y ago)39120.0k↓49.1%77[7 issues](https://github.com/overtrue/laravel-shopping-cart/issues)MITPHPPHP &gt;=5.5

Since Jun 1Pushed 5y ago1 watchersCompare

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

READMEChangelog (9)Dependencies (3)Versions (10)Used By (0)

Laravel Shopping Cart
=====================

[](#laravel-shopping-cart)

Shopping cart for Laravel Application.

[![Build Status](https://camo.githubusercontent.com/4f31347d7007c436f512f328f111874f7738a23b8ebccedfad8475bea314897f/68747470733a2f2f7472617669732d63692e6f72672f6f766572747275652f6c61726176656c2d73686f7070696e672d636172742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/overtrue/laravel-shopping-cart)[![Latest Stable Version](https://camo.githubusercontent.com/a945170e92f8bf503cbddab7458be133d3ba73256b84dea510ba529ec20f3a30/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d73686f7070696e672d636172742f762f737461626c652e737667)](https://packagist.org/packages/overtrue/laravel-shopping-cart)[![Latest Unstable Version](https://camo.githubusercontent.com/e23bf2e609ff115be9430c440ae01f420a91fbce9266f1b7982142eabd5ff25b/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d73686f7070696e672d636172742f762f756e737461626c652e737667)](https://packagist.org/packages/overtrue/laravel-shopping-cart)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/6aaf45e34b041f2ec36e5d70c9385466ec6ea3ba30ef9eba8b68ed12ce3b3d92/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f766572747275652f6c61726176656c2d73686f7070696e672d636172742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/overtrue/laravel-shopping-cart/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/5cdd5f0ae1e88c07b0b651d18e5e1b4c678c0c2931fced75182d6eedaadf1470/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f766572747275652f6c61726176656c2d73686f7070696e672d636172742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/overtrue/laravel-shopping-cart/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/bdb0665346b8327a10ae71c82b9b10ecfc55ede509fcb8af2ce2cd705654ef51/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d73686f7070696e672d636172742f646f776e6c6f616473)](https://packagist.org/packages/overtrue/laravel-shopping-cart)[![License](https://camo.githubusercontent.com/1ef6b3531ca6c7abeafd7f243ea8ff968631ac1da8d181e03cb3d71eda138dd7/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d73686f7070696e672d636172742f6c6963656e7365)](https://packagist.org/packages/overtrue/wechat)

Installation
============

[](#installation)

```
$ composer require "overtrue/laravel-shopping-cart:~2.0"
```

or add the following line to your project's `composer.json`:

```
"require": {
    "overtrue/laravel-shopping-cart": "~2.0"
}
```

then

```
$ composer update
```

After completion of the above, add the follow line to the section `providers` of `config/app.php`:

```
Overtrue\LaravelShoppingCart\ServiceProvider::class,
```

And add the follow line to the section `aliases`:

```
'ShoppingCart'      => Overtrue\LaravelShoppingCart\Facade::class,
```

Usage
=====

[](#usage)

### Add item to cart

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

Add a new item.

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

**example:**

```
$row = ShoppingCart::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 ShoppingCart::update(string $rawId, int $quantity);
Item ShoppingCart::update(string $rawId, array $arrtibutes);
```

**example:**

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

### Get all items

[](#get-all-items)

Get all the items.

```
Collection ShoppingCart::all();
```

**example:**

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

### Get item

[](#get-item)

Get the specified item.

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

**example:**

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

### Remove item

[](#remove-item)

Remove the specified item by raw ID.

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

**example:**

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

### Destroy cart

[](#destroy-cart)

Clean Shopping Cart.

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

**example:**

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

### Total price

[](#total-price)

Returns the total of all items.

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

**example:**

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

### Count rows

[](#count-rows)

Return the number of rows.

```
int ShoppingCart::countRows();
```

**example:**

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

### Count quantity

[](#count-quantity)

Returns the quantity of all items

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

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

**example:**

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

### Search items

[](#search-items)

Search items by property.

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

**example:**

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

### Check empty

[](#check-empty)

```
bool ShoppingCart::isEmpty();
```

### Specifies the associated model

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

Specifies the associated model of item before you add items to cart.

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

**example:**

```
ShoppingCart::associate('App\Models\Product');

ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);

$item = ShoppingCart::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`shopping_cart.adding`($attributes, $cart);`shopping_cart.added`($attributes, $cart);`shopping_cart.updating`($row, $cart);`shopping_cart.updated`($row, $cart);`shopping_cart.removing`($row, $cart);`shopping_cart.removed`($row, $cart);`shopping_cart.destroying`($cart);`shopping_cart.destroyed`($cart);You can easily handle these events, for example:

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

PHP 扩展包开发
---------

[](#php-扩展包开发)

> 想知道如何从零开始构建 PHP 扩展包？
>
> 请关注我的实战课程，我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)

License
=======

[](#license)

MIT

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 81.4% 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 ~199 days

Recently: every ~265 days

Total

9

Last Release

2457d ago

Major Versions

1.0.5 → 2.0.02017-07-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/c507e57eab402e81335012357b7d7df6c5cafda3073adcc94b475037127d263f?d=identicon)[overtrue](/maintainers/overtrue)

---

Top Contributors

[![overtrue](https://avatars.githubusercontent.com/u/1472352?v=4)](https://github.com/overtrue "overtrue (92 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (9 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (6 commits)")[![qbhy](https://avatars.githubusercontent.com/u/24204533?v=4)](https://github.com/qbhy "qbhy (2 commits)")[![jaychan](https://avatars.githubusercontent.com/u/3414568?v=4)](https://github.com/jaychan "jaychan (1 commits)")[![m809745357](https://avatars.githubusercontent.com/u/15150683?v=4)](https://github.com/m809745357 "m809745357 (1 commits)")[![summerblue](https://avatars.githubusercontent.com/u/324764?v=4)](https://github.com/summerblue "summerblue (1 commits)")[![uax](https://avatars.githubusercontent.com/u/10324413?v=4)](https://github.com/uax "uax (1 commits)")

---

Tags

laravelshopping-cartlaravelshoppingshopping cart

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[realrashid/cart

Meet Cart — your ultimate solution for seamless shopping cart functionality in Laravel applications. Cart simplifies the complexities of shopping cart operations, from product additions to total calculations, ensuring a frictionless user experience.

1104.1k](/packages/realrashid-cart)[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)

PHPackages © 2026

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