PHPackages                             nguyenquang2302/shoppingcart-eloquent - 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. [Database &amp; ORM](/categories/database)
4. /
5. nguyenquang2302/shoppingcart-eloquent

ActiveLibrary[Database &amp; ORM](/categories/database)

nguyenquang2302/shoppingcart-eloquent
=====================================

A simple Laravel Based Shopping Cart. Uses persistent Eloquent Databases instead of Sessions.

0107PHP

Since May 20Pushed 4y ago1 watchersCompare

[ Source](https://github.com/nguyenquang2302/ShoppingCart)[ Packagist](https://packagist.org/packages/nguyenquang2302/shoppingcart-eloquent)[ RSS](/packages/nguyenquang2302-shoppingcart-eloquent/feed)WikiDiscussions main Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Eloquent Shopping Cart
======================

[](#eloquent-shopping-cart)

A simple Laravel Based Shopping Cart. Uses persistent Eloquent Databases instead of Sessions.
[![Latest Stable Version](https://camo.githubusercontent.com/0df8c4f264d00fcc0629147d864727503139e2efd03c6cd9080ca6c569ac3ffc/68747470733a2f2f706f7365722e707567782e6f72672f6761757261766f6a68612f73686f7070696e67636172742d656c6f7175656e742f762f737461626c65)](https://packagist.org/packages/gauravojha/shoppingcart-eloquent)[![Total Downloads](https://camo.githubusercontent.com/6a5bbe02e89fcf6bd4f2909194a33b6f1e1090987be066f740835a710043f28b/68747470733a2f2f706f7365722e707567782e6f72672f6761757261766f6a68612f73686f7070696e67636172742d656c6f7175656e742f646f776e6c6f616473)](https://packagist.org/packages/gauravojha/shoppingcart-eloquent)[![Build Status](https://camo.githubusercontent.com/d665bd72a751f92a10893bea794f0e1b2bf7317aff20c32e706821930ad08d48/68747470733a2f2f7472617669732d63692e6f72672f6761757261766f6a68612f73686f7070696e67636172742d656c6f7175656e742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/gauravojha/shoppingcart-eloquent)

### Note

[](#note)

If you have a couple minutes, I would request you to read the [motivation](#motivation) behind why I chose to make this package in the first place, and why this is different from the other Laravel based packages available. But of course, you may choose to dive right in!

Another thing, I know that the test cases are in shambles. I aim to fix that soon.

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

[](#installation)

You can install the package through [Composer](https://getcomposer.org).

Run the Composer require command from the Terminal:

```
composer require gauravojha/shoppingcart-eloquent

```

Next up, add the service provider of the package and alias the package. Open the `config/app.php` file and:

- add the following line to the `providers` array

    > Ndq\\Shoppingcart\\ShoppingCartProvider::class,
- add the following line to the `aliases` array

    > 'Cart' =&gt; Ndq\\Shoppingcart\\CartFacade::class,

Since this package uses the Eloquent databases and an associated Model, you also need to run the following command from the root of your application:

```
> php artisan vendor:publish --provider="Ndq\Shoppingcart\ShoppingCartProvider"

```

And of course, it goes without saying, run the migrations after this to create the database using `php artisan migrate`

With the above steps done and dusted, you are ready to start using this shopping cart.

Usage
=====

[](#usage)

All the usage scenarios below are explained with a single Controller at the end in the [Examples](#examples) section

The methods below use the model `App\ShoppingCartItem`, which is automatically provided for you as part of the `vendor:publish` command.

The shopping cart gives you the following methods to use:

**Cart::addToCart($productid, $price, $quantity)**

Adds the desired number or `$quantity` of the passed product (identified by its unique `$productid`) to the shopping cart. The `$price` in this implementation is the price *of a single item* and not the total cost of all similar items in the cart.

```
// get a product to add to the cart
// this will fetch from the database a product, which has 'id' equal to $id
$product = Product::find($id);
// now add 4 such products to the cart
Cart::addToCart($product->id, $product->price, 4);
```

If the same product for the user already exists in the cart, the quantity would be updated, instead of creating a new record.

**Cart::content()**

Will return all the contents of the shopping cart to the user. Once you have the content, you can retrieve the 'product\_id' from it, and further retrieve the products based on the id from the database and perform operations on it.

```
// obtain all the products in the cart
$contents = Cart::content();

// obtain the no.of products in the cart
$count = Cart::content()->count();
```

**Cart::removeFromCart($productid)**

Will remove the product from the cart whose `id` is equal to the passed `$productid`

```
// if a product exists in the cart which has a $product->id = 4, then it will be removed from the cart in its entirety
Cart::removeFromCart(4);
```

**Cart::emptyCart()**

Destroys all the contents of the shopping cart.

```
// remove all the products in the shopping cart
Cart::emptyCart();
```

**Cart::updateItemInCart($productid, $quantity)**

Updates the `$quantity` of the product present in the shopping cart whose product-&gt;id equals `$productid`.

```
// change the quantity of products.
// Assuming the cart has a product which has a product with id '4', and you want to change its quantity to 1.
Cart::updateItemInCart(4, 1);
```

**Cart::totalPrice()**

Every application may have a different process to calculate the total cost of the items in the cart. In this implementation, inside `ShoppingCartImpl` file, I have declared two private variables `$taxRate` and `$deliveryCharges` set to some defaults. The method `totalPrice` calculates the total price the customer has to pay during checkout, by first adding the total cost of all items in the cart, then adding the tax and delivery charges over it. The code is pretty straightforward and simple, so it shouldnt prove hard to modify it and use it as per your requirement.

```
$totalPrice = Cart::totalPrice();
```

That is about it. Since the records are inserted using the authenticated user id, there is no chance of a conflict or other users able to view your records. Moreover, you can add products, go for that week long vacation, and since the data is persisted, find the products right where you left them.

Examples
========

[](#examples)

As already mentioned, this package (if you use as it is), imposes some basic constraints/assumptions over you.

For this section:

- assume a simple table 'products' with the following structure ```
