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

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

asyrafhussin/laravel-shopping-cart
==================================

Shopping cart for Laravel Application.

2.0.2(6y ago)18MITPHPPHP &gt;=5.5

Since Jun 1Pushed 6y agoCompare

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

READMEChangelog (2)Dependencies (3)Versions (12)Used By (0)

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

[](#laravel-shopping-cart)

Shopping cart for Laravel Application.

[![Build Status](https://camo.githubusercontent.com/60b33a99311769ab44ae5240ca8476fd7229cc97cc467bc2b2968ed144385ff8/68747470733a2f2f7472617669732d63692e6f72672f41737972616648757373696e2f6c61726176656c2d73686f7070696e672d636172742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/AsyrafHussin/laravel-shopping-cart)[![Latest Stable Version](https://camo.githubusercontent.com/07d14e5cd2d09567a8b9570532266dc2e2c96ace1ed86ce75cd7c33f43ba5391/68747470733a2f2f706f7365722e707567782e6f72672f41737972616648757373696e2f6c61726176656c2d73686f7070696e672d636172742f762f737461626c652e737667)](https://packagist.org/packages/AsyrafHussin/laravel-shopping-cart)[![Latest Unstable Version](https://camo.githubusercontent.com/d23a6e4d31aa89c7ab89ac0635dce483dc84356c5d505f8734c1796c120145f1/68747470733a2f2f706f7365722e707567782e6f72672f41737972616648757373696e2f6c61726176656c2d73686f7070696e672d636172742f762f756e737461626c652e737667)](https://packagist.org/packages/AsyrafHussin/laravel-shopping-cart)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/d716e670b62918a672d7a6ea77dff347179e94225619d5c9001a357ec748f2da/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f41737972616648757373696e2f6c61726176656c2d73686f7070696e672d636172742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/AsyrafHussin/laravel-shopping-cart/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/d067f7f98123f1a1266d48b99643e0cde2ef6620c8b7f0bd8b1d4e5e3fcf4396/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f41737972616648757373696e2f6c61726176656c2d73686f7070696e672d636172742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/AsyrafHussin/laravel-shopping-cart/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/0691b6e1d184d5cad01c296af4247a92fc35e20dd7580260bb743c7cd7b749bb/68747470733a2f2f706f7365722e707567782e6f72672f41737972616648757373696e2f6c61726176656c2d73686f7070696e672d636172742f646f776e6c6f616473)](https://packagist.org/packages/AsyrafHussin/laravel-shopping-cart)[![License](https://camo.githubusercontent.com/b9781d2f36b3c3b5d6c6b1e7b329dae8873bae230ee9b9bca5e233613b6b55c6/68747470733a2f2f706f7365722e707567782e6f72672f41737972616648757373696e2f6c61726176656c2d73686f7070696e672d636172742f6c6963656e7365)](https://packagist.org/packages/AsyrafHussin/wechat)

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

[](#installation)

```
$ composer require "asyrafhussin/laravel-shopping-cart:~2.0.2@dev"
```

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

```
"require": {
    "asyrafhussin/laravel-shopping-cart": "~2.0.2@dev"
}
```

then

```
$ composer update
```

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

```
AsyrafHussin\LaravelShoppingCart\ServiceProvider::class,
```

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

```
'ShoppingCart'      => AsyrafHussin\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 `AsyrafHussin\LaravelShoppingCart\Item` are instanceof `Illuminate\Support\Collection`, Usage Refer to：[Collections - Laravel doc.](http://laravel.com/docs/5.0/collections)

properties of `AsyrafHussin\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

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 81.6% 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 ~193 days

Recently: every ~255 days

Total

9

Last Release

2449d ago

Major Versions

1.0.5 → 2.0.02017-07-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/2882b80940e6806e2d0785c834a0c3002013cfdfbd01d26094edc4aaf0df01e0?d=identicon)[AsyrafHussin](/maintainers/AsyrafHussin)

---

Top Contributors

[![overtrue](https://avatars.githubusercontent.com/u/1472352?v=4)](https://github.com/overtrue "overtrue (80 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (6 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (4 commits)")[![AsyrafHussin](https://avatars.githubusercontent.com/u/10107885?v=4)](https://github.com/AsyrafHussin "AsyrafHussin (4 commits)")[![uax](https://avatars.githubusercontent.com/u/10324413?v=4)](https://github.com/uax "uax (1 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)")

---

Tags

laravelshoppingshopping cart

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/asyrafhussin-laravel-shopping-cart/health.svg)](https://phpackages.com/packages/asyrafhussin-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)
