[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-8482":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":16,"stars7d":16,"stars30d":17,"stars90d":16,"forks30d":16,"starsTrendScore":16,"compositeScore":18,"rankGlobal":10,"rankLanguage":10,"license":19,"archived":20,"fork":20,"defaultBranch":21,"hasWiki":22,"hasPages":20,"topics":23,"createdAt":10,"pushedAt":10,"updatedAt":24,"readmeContent":25,"aiSummary":26,"trendingCount":16,"starSnapshotCount":16,"syncStatus":27,"lastSyncTime":28,"discoverSource":29},8482,"LaravelShoppingcart","Crinsane\u002FLaravelShoppingcart","Crinsane","A simple shopping cart implementation for Laravel","",null,"PHP",3704,1762,192,181,0,3,31.74,"MIT License",false,"master",true,[],"2026-06-12 02:01:54","## LaravelShoppingcart\n[![Build Status](https:\u002F\u002Ftravis-ci.org\u002FCrinsane\u002FLaravelShoppingcart.png?branch=master)](https:\u002F\u002Ftravis-ci.org\u002FCrinsane\u002FLaravelShoppingcart)\n[![Total Downloads](https:\u002F\u002Fposer.pugx.org\u002Fgloudemans\u002Fshoppingcart\u002Fdownloads.png)](https:\u002F\u002Fpackagist.org\u002Fpackages\u002Fgloudemans\u002Fshoppingcart)\n[![Latest Stable Version](https:\u002F\u002Fposer.pugx.org\u002Fgloudemans\u002Fshoppingcart\u002Fv\u002Fstable)](https:\u002F\u002Fpackagist.org\u002Fpackages\u002Fgloudemans\u002Fshoppingcart)\n[![Latest Unstable Version](https:\u002F\u002Fposer.pugx.org\u002Fgloudemans\u002Fshoppingcart\u002Fv\u002Funstable)](https:\u002F\u002Fpackagist.org\u002Fpackages\u002Fgloudemans\u002Fshoppingcart)\n[![License](https:\u002F\u002Fposer.pugx.org\u002Fgloudemans\u002Fshoppingcart\u002Flicense)](https:\u002F\u002Fpackagist.org\u002Fpackages\u002Fgloudemans\u002Fshoppingcart)\n\nA simple shoppingcart implementation for Laravel.\n\n## Installation\n\nInstall the package through [Composer](http:\u002F\u002Fgetcomposer.org\u002F). \n\nRun the Composer require command from the Terminal:\n\n    composer require gloudemans\u002Fshoppingcart\n    \nIf you're using Laravel 5.5, this is all there is to do. \n\nShould you still be on version 5.4 of Laravel, the final steps for you are to add the service provider of the package and alias the package. To do this open your `config\u002Fapp.php` file.\n\nAdd a new line to the `providers` array:\n\n\tGloudemans\\Shoppingcart\\ShoppingcartServiceProvider::class\n\nAnd optionally add a new line to the `aliases` array:\n\n\t'Cart' => Gloudemans\\Shoppingcart\\Facades\\Cart::class,\n\nNow you're ready to start using the shoppingcart in your application.\n\n**As of version 2 of this package it's possibly to use dependency injection to inject an instance of the Cart class into your controller or other class**\n\n## Overview\nLook at one of the following topics to learn more about LaravelShoppingcart\n\n* [Usage](#usage)\n* [Collections](#collections)\n* [Instances](#instances)\n* [Models](#models)\n* [Database](#database)\n* [Exceptions](#exceptions)\n* [Events](#events)\n* [Example](#example)\n\n## Usage\n\nThe shoppingcart gives you the following methods to use:\n\n### Cart::add()\n\nAdding an item to the cart is really simple, you just use the `add()` method, which accepts a variety of parameters.\n\nIn its most basic form you can specify the id, name, quantity, price of the product you'd like to add to the cart.\n\n```php\nCart::add('293ad', 'Product 1', 1, 9.99);\n```\n\nAs an optional fifth parameter you can pass it options, so you can add multiple items with the same id, but with (for instance) a different size.\n\n```php\nCart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);\n```\n\n**The `add()` method will return an CartItem instance of the item you just added to the cart.**\n\nMaybe you prefer to add the item using an array? As long as the array contains the required keys, you can pass it to the method. The options key is optional.\n\n```php\nCart::add(['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => ['size' => 'large']]);\n```\n\nNew in version 2 of the package is the possibility to work with the `Buyable` interface. The way this works is that you have a model implement the `Buyable` interface, which will make you implement a few methods so the package knows how to get the id, name and price from your model. \nThis way you can just pass the `add()` method a model and the quantity and it will automatically add it to the cart. \n\n**As an added bonus it will automatically associate the model with the CartItem**\n\n```php\nCart::add($product, 1, ['size' => 'large']);\n```\nAs an optional third parameter you can add options.\n```php\nCart::add($product, 1, ['size' => 'large']);\n```\n\nFinally, you can also add multipe items to the cart at once.\nYou can just pass the `add()` method an array of arrays, or an array of Buyables and they will be added to the cart. \n\n**When adding multiple items to the cart, the `add()` method will return an array of CartItems.**\n\n```php\nCart::add([\n  ['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00],\n  ['id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => ['size' => 'large']]\n]);\n\nCart::add([$product1, $product2]);\n\n```\n\n### Cart::update()\n\nTo update an item in the cart, you'll first need the rowId of the item.\nNext you can use the `update()` method to update it.\n\nIf you simply want to update the quantity, you'll pass the update method the rowId and the new quantity:\n\n```php\n$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';\n\nCart::update($rowId, 2); \u002F\u002F Will update the quantity\n```\n\nIf you want to update more attributes of the item, you can either pass the update method an array or a `Buyable` as the second parameter. This way you can update all information of the item with the given rowId.\n\n```php\nCart::update($rowId, ['name' => 'Product 1']); \u002F\u002F Will update the name\n\nCart::update($rowId, $product); \u002F\u002F Will update the id, name and price\n\n```\n\n### Cart::remove()\n\nTo remove an item for the cart, you'll again need the rowId. This rowId you simply pass to the `remove()` method and it will remove the item from the cart.\n\n```php\n$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';\n\nCart::remove($rowId);\n```\n\n### Cart::get()\n\nIf you want to get an item from the cart using its rowId, you can simply call the `get()` method on the cart and pass it the rowId.\n\n```php\n$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';\n\nCart::get($rowId);\n```\n\n### Cart::content()\n\nOf course you also want to get the carts content. This is where you'll use the `content` method. This method will return a Collection of CartItems which you can iterate over and show the content to your customers.\n\n```php\nCart::content();\n```\n\nThis method will return the content of the current cart instance, if you want the content of another instance, simply chain the calls.\n\n```php\nCart::instance('wishlist')->content();\n```\n\n### Cart::destroy()\n\nIf you want to completely remove the content of a cart, you can call the destroy method on the cart. This will remove all CartItems from the cart for the current cart instance.\n\n```php\nCart::destroy();\n```\n\n### Cart::total()\n\nThe `total()` method can be used to get the calculated total of all items in the cart, given there price and quantity.\n\n```php\nCart::total();\n```\n\nThe method will automatically format the result, which you can tweak using the three optional parameters\n\n```php\nCart::total($decimals, $decimalSeperator, $thousandSeperator);\n```\n\nYou can set the default number format in the config file.\n\n**If you're not using the Facade, but use dependency injection in your (for instance) Controller, you can also simply get the total property `$cart->total`**\n\n### Cart::tax()\n\nThe `tax()` method can be used to get the calculated amount of tax for all items in the cart, given there price and quantity.\n\n```php\nCart::tax();\n```\n\nThe method will automatically format the result, which you can tweak using the three optional parameters\n\n```php\nCart::tax($decimals, $decimalSeperator, $thousandSeperator);\n```\n\nYou can set the default number format in the config file.\n\n**If you're not using the Facade, but use dependency injection in your (for instance) Controller, you can also simply get the tax property `$cart->tax`**\n\n### Cart::subtotal()\n\nThe `subtotal()` method can be used to get the total of all items in the cart, minus the total amount of tax. \n\n```php\nCart::subtotal();\n```\n\nThe method will automatically format the result, which you can tweak using the three optional parameters\n\n```php\nCart::subtotal($decimals, $decimalSeperator, $thousandSeperator);\n```\n\nYou can set the default number format in the config file.\n\n**If you're not using the Facade, but use dependency injection in your (for instance) Controller, you can also simply get the subtotal property `$cart->subtotal`**\n\n### Cart::count()\n\nIf you want to know how many items there are in your cart, you can use the `count()` method. This method will return the total number of items in the cart. So if you've added 2 books and 1 shirt, it will return 3 items.\n\n```php\nCart::count();\n```\n\n### Cart::search()\n\nTo find an item in the cart, you can use the `search()` method.\n\n**This method was changed on version 2**\n\nBehind the scenes, the method simply uses the filter method of the Laravel Collection class. This means you must pass it a Closure in which you'll specify you search terms.\n\nIf you for instance want to find all items with an id of 1:\n\n```php\n$cart->search(function ($cartItem, $rowId) {\n\treturn $cartItem->id === 1;\n});\n```\n\nAs you can see the Closure will receive two parameters. The first is the CartItem to perform the check against. The second parameter is the rowId of this CartItem.\n\n**The method will return a Collection containing all CartItems that where found**\n\nThis way of searching gives you total control over the search process and gives you the ability to create very precise and specific searches.\n\n## Collections\n\nOn multiple instances the Cart will return to you a Collection. This is just a simple Laravel Collection, so all methods you can call on a Laravel Collection are also available on the result.\n\nAs an example, you can quicky get the number of unique products in a cart:\n\n```php\nCart::content()->count();\n```\n\nOr you can group the content by the id of the products:\n\n```php\nCart::content()->groupBy('id');\n```\n\n## Instances\n\nThe packages supports multiple instances of the cart. The way this works is like this:\n\nYou can set the current instance of the cart by calling `Cart::instance('newInstance')`. From this moment, the active instance of the cart will be `newInstance`, so when you add, remove or get the content of the cart, you're work with the `newInstance` instance of the cart.\nIf you want to switch instances, you just call `Cart::instance('otherInstance')` again, and you're working with the `otherInstance` again.\n\nSo a little example:\n\n```php\nCart::instance('shopping')->add('192ao12', 'Product 1', 1, 9.99);\n\n\u002F\u002F Get the content of the 'shopping' cart\nCart::content();\n\nCart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, ['size' => 'medium']);\n\n\u002F\u002F Get the content of the 'wishlist' cart\nCart::content();\n\n\u002F\u002F If you want to get the content of the 'shopping' cart again\nCart::instance('shopping')->content();\n\n\u002F\u002F And the count of the 'wishlist' cart again\nCart::instance('wishlist')->count();\n```\n\n**N.B. Keep in mind that the cart stays in the last set instance for as long as you don't set a different one during script execution.**\n\n**N.B.2 The default cart instance is called `default`, so when you're not using instances,`Cart::content();` is the same as `Cart::instance('default')->content()`.**\n\n## Models\n\nBecause it can be very convenient to be able to directly access a model from a CartItem is it possible to associate a model with the items in the cart. Let's say you have a `Product` model in your application. With the `associate()` method, you can tell the cart that an item in the cart, is associated to the `Product` model. \n\nThat way you can access your model right from the `CartItem`!\n\nThe model can be accessed via the `model` property on the CartItem.\n\n**If your model implements the `Buyable` interface and you used your model to add the item to the cart, it will associate automatically.**\n\nHere is an example:\n\n```php\n\n\u002F\u002F First we'll add the item to the cart.\n$cartItem = Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);\n\n\u002F\u002F Next we associate a model with the item.\nCart::associate($cartItem->rowId, 'Product');\n\n\u002F\u002F Or even easier, call the associate method on the CartItem!\n$cartItem->associate('Product');\n\n\u002F\u002F You can even make it a one-liner\nCart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large'])->associate('Product');\n\n\u002F\u002F Now, when iterating over the content of the cart, you can access the model.\nforeach(Cart::content() as $row) {\n\techo 'You have ' . $row->qty . ' items of ' . $row->model->name . ' with description: \"' . $row->model->description . '\" in your cart.';\n}\n```\n## Database\n\n- [Config](#configuration)\n- [Storing the cart](#save-cart-to-database)\n- [Restoring the cart](#retrieve-cart-from-database)\n\n### Configuration\nTo save cart into the database so you can retrieve it later, the package needs to know which database connection to use and what the name of the table is.\nBy default the package will use the default database connection and use a table named `shoppingcart`.\nIf you want to change these options, you'll have to publish the `config` file.\n\n    php artisan vendor:publish --provider=\"Gloudemans\\Shoppingcart\\ShoppingcartServiceProvider\" --tag=\"config\"\n\nThis will give you a `cart.php` config file in which you can make the changes.\n\nTo make your life easy, the package also includes a ready to use `migration` which you can publish by running:\n\n    php artisan vendor:publish --provider=\"Gloudemans\\Shoppingcart\\ShoppingcartServiceProvider\" --tag=\"migrations\"\n    \nThis will place a `shoppingcart` table's migration file into `database\u002Fmigrations` directory. Now all you have to do is run `php artisan migrate` to migrate your database.\n\n### Storing the cart    \nTo store your cart instance into the database, you have to call the `store($identifier) ` method. Where `$identifier` is a random key, for instance the id or username of the user.\n\n    Cart::store('username');\n    \n    \u002F\u002F To store a cart instance named 'wishlist'\n    Cart::instance('wishlist')->store('username');\n\n### Restoring the cart\nIf you want to retrieve the cart from the database and restore it, all you have to do is call the  `restore($identifier)` where `$identifier` is the key you specified for the `store` method.\n \n    Cart::restore('username');\n    \n    \u002F\u002F To restore a cart instance named 'wishlist'\n    Cart::instance('wishlist')->restore('username');\n\n## Exceptions\n\nThe Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions:\n\n| Exception                    | Reason                                                                             |\n| ---------------------------- | ---------------------------------------------------------------------------------- |\n| *CartAlreadyStoredException* | When trying to store a cart that was already stored using the specified identifier |\n| *InvalidRowIDException*      | When the rowId that got passed doesn't exists in the current cart instance         |\n| *UnknownModelException*      | When you try to associate an none existing model to a CartItem.                    |\n\n## Events\n\nThe cart also has events build in. There are five events available for you to listen for.\n\n| Event         | Fired                                    | Parameter                        |\n| ------------- | ---------------------------------------- | -------------------------------- |\n| cart.added    | When an item was added to the cart.      | The `CartItem` that was added.   |\n| cart.updated  | When an item in the cart was updated.    | The `CartItem` that was updated. |\n| cart.removed  | When an item is removed from the cart.   | The `CartItem` that was removed. |\n| cart.stored   | When the content of a cart was stored.   | -                                |\n| cart.restored | When the content of a cart was restored. | -                                |\n\n## Example\n\nBelow is a little example of how to list the cart content in a table:\n\n```php\n\n\u002F\u002F Add some items in your Controller.\nCart::add('192ao12', 'Product 1', 1, 9.99);\nCart::add('1239ad0', 'Product 2', 2, 5.95, ['size' => 'large']);\n\n\u002F\u002F Display the content in a View.\n\u003Ctable>\n   \t\u003Cthead>\n       \t\u003Ctr>\n           \t\u003Cth>Product\u003C\u002Fth>\n           \t\u003Cth>Qty\u003C\u002Fth>\n           \t\u003Cth>Price\u003C\u002Fth>\n           \t\u003Cth>Subtotal\u003C\u002Fth>\n       \t\u003C\u002Ftr>\n   \t\u003C\u002Fthead>\n\n   \t\u003Ctbody>\n\n   \t\t\u003C?php foreach(Cart::content() as $row) :?>\n\n       \t\t\u003Ctr>\n           \t\t\u003Ctd>\n               \t\t\u003Cp>\u003Cstrong>\u003C?php echo $row->name; ?>\u003C\u002Fstrong>\u003C\u002Fp>\n               \t\t\u003Cp>\u003C?php echo ($row->options->has('size') ? $row->options->size : ''); ?>\u003C\u002Fp>\n           \t\t\u003C\u002Ftd>\n           \t\t\u003Ctd>\u003Cinput type=\"text\" value=\"\u003C?php echo $row->qty; ?>\">\u003C\u002Ftd>\n           \t\t\u003Ctd>$\u003C?php echo $row->price; ?>\u003C\u002Ftd>\n           \t\t\u003Ctd>$\u003C?php echo $row->total; ?>\u003C\u002Ftd>\n       \t\t\u003C\u002Ftr>\n\n\t   \t\u003C?php endforeach;?>\n\n   \t\u003C\u002Ftbody>\n   \t\n   \t\u003Ctfoot>\n   \t\t\u003Ctr>\n   \t\t\t\u003Ctd colspan=\"2\">&nbsp;\u003C\u002Ftd>\n   \t\t\t\u003Ctd>Subtotal\u003C\u002Ftd>\n   \t\t\t\u003Ctd>\u003C?php echo Cart::subtotal(); ?>\u003C\u002Ftd>\n   \t\t\u003C\u002Ftr>\n   \t\t\u003Ctr>\n   \t\t\t\u003Ctd colspan=\"2\">&nbsp;\u003C\u002Ftd>\n   \t\t\t\u003Ctd>Tax\u003C\u002Ftd>\n   \t\t\t\u003Ctd>\u003C?php echo Cart::tax(); ?>\u003C\u002Ftd>\n   \t\t\u003C\u002Ftr>\n   \t\t\u003Ctr>\n   \t\t\t\u003Ctd colspan=\"2\">&nbsp;\u003C\u002Ftd>\n   \t\t\t\u003Ctd>Total\u003C\u002Ftd>\n   \t\t\t\u003Ctd>\u003C?php echo Cart::total(); ?>\u003C\u002Ftd>\n   \t\t\u003C\u002Ftr>\n   \t\u003C\u002Ftfoot>\n\u003C\u002Ftable>\n```\n","LaravelShoppingcart 是一个为 Laravel 框架设计的简单购物车实现。它提供了基本的购物车功能，如添加商品、处理数量和价格等，并支持通过数组或 Buyable 接口添加商品项，使得开发者能够灵活地集成购物车到自己的应用中。此外，该库还允许使用依赖注入来管理购物车实例，增强了代码的可测试性和模块化程度。适用于需要快速搭建电子商务功能或者任何涉及到购物车逻辑的 Laravel 项目。",2,"2026-06-11 03:18:15","top_language"]