{"id":24199063,"url":"https://github.com/isap-ou/laravel-cart","last_synced_at":"2026-04-17T08:01:53.119Z","repository":{"id":263494252,"uuid":"890376942","full_name":"isap-ou/laravel-cart","owner":"isap-ou","description":"Laravel Cart is a highly customizable package that enables you to easily add shopping cart functionality to your Laravel applications. ","archived":false,"fork":false,"pushed_at":"2025-09-19T07:18:42.000Z","size":363,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-05T00:13:42.510Z","etag":null,"topics":["cart","ecommerce","laravel","library","redis"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/isap-ou.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-11-18T13:18:01.000Z","updated_at":"2025-09-19T07:18:45.000Z","dependencies_parsed_at":"2024-11-18T21:50:58.492Z","dependency_job_id":"ac1f5c5e-ea77-4f80-ab7e-ad9e30882416","html_url":"https://github.com/isap-ou/laravel-cart","commit_stats":null,"previous_names":["isap-ou/laravel-cart"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/isap-ou/laravel-cart","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isap-ou%2Flaravel-cart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isap-ou%2Flaravel-cart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isap-ou%2Flaravel-cart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isap-ou%2Flaravel-cart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/isap-ou","download_url":"https://codeload.github.com/isap-ou/laravel-cart/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isap-ou%2Flaravel-cart/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31920518,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T18:22:33.417Z","status":"online","status_checked_at":"2026-04-17T02:00:06.879Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cart","ecommerce","laravel","library","redis"],"created_at":"2025-01-13T20:19:59.279Z","updated_at":"2026-04-17T08:01:53.104Z","avatar_url":"https://github.com/isap-ou.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Cart\n**Laravel Cart** is a highly customizable package that enables you to easily add shopping cart functionality to your Laravel applications. With flexible options for item management, persistent storage, and deep integration with Laravel, it is perfect for building e-commerce or custom shopping features.\n\n[![Laravel cart](https://github.com/isap-ou/laravel-cart/blob/main/images/banner.jpg?raw=true)](https://github.com/isap-ou/laravel-cart)\n\n[![Total Downloads](https://img.shields.io/packagist/dt/isapp/laravel-cart.svg?style=flat-square)](https://packagist.org/packages/isapp/laravel-cart)\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/isapp/laravel-cart.svg?style=flat-square)](https://packagist.org/packages/isapp/laravel-cart)\n\n## Features\n- ✅ Persistent cart storage (database/session)\n- ✅ Guest and authenticated user support\n- ✅ High precision price calculations\n- ✅ Model associations\n- ✅ Method chaining\n- ✅ Exception handling\n\n## Installation\n\nTo install the package, use Composer:\n\n```bash\ncomposer require isapp/laravel-cart\n```\n\n## Publishing Configuration\n\nTo modify the default configuration, publish the configuration file using the following Artisan command:\n\n```bash\nphp artisan vendor:publish --provider=\"Isapp\\LaravelCart\\CartServiceProvider\" --tag=\"config\"\n```\n\nThis command will create a `config/laravel-cart.php` file where you can customize package settings as needed.\n\n## Publishing and Running Migrations\n\nTo publish the migration files provided by the `Laravel Cart` package, use the following Artisan command:\n\n```bash\nphp artisan vendor:publish --provider=\"Isapp\\LaravelCart\\CartServiceProvider\" --tag=\"migrations\"\n```\n\nThis command will copy the migration files to your project's `database/migrations` directory.\n\n\n### Running the Migrations\n\nOnce the migration files are published, you can apply the migrations to your database using the following command:\n\n```bash\nphp artisan migrate\n```\n\n## Getting Started\n\n### User Management\n\n#### `getUser(): ?Authenticatable`\nReturns the currently authenticated user or null if no user is set.\n\n```php\n$user = Cart::getUser();\n```\n\n#### `setUser(Authenticatable $user): Driver`\nSets a specific user for cart operations. Useful for admin operations or impersonation.\n\n```php\n$user = User::find(1);\nCart::setUser($user)-\u003estoreItem($item);\n```\n\n#### `setGuard(string $guard): Driver`\nSets the authentication guard to use (default is 'web').\n\n```php\nCart::setGuard('admin')-\u003egetUser();\n```\n\n### Cart Management\n\n#### `get(): Model|Cart`\nGets or creates the cart for the current user or session. For authenticated users, finds or creates cart by user_id. For guests, uses session_id.\n\n```php\n$cart = Cart::get();\necho \"Cart has \" . $cart-\u003eitems-\u003ecount() . \" items\";\n```\n\n### Item Storage\n\n#### `storeItem(CartItemContract $item): Driver`\nStores a single item in the cart. If the item already exists, increases its quantity instead.\n\n```php\n$product = Product::find(1);\n$cartItem = new CartItem();\n$cartItem-\u003eitemable()-\u003eassociate($product);\nCart::storeItem($cartItem);\n```\n\n**Throws:**\n- `NotImplementedException` - if itemable doesn't implement CartItemProduct\n- `ItemAssociatedWithDifferentCartException` - if trying to add item from another cart\n\n#### `storeItems(Collection $items): static`\nStores multiple items in the cart at once.\n\n```php\n$items = collect([\n    $cartItem1,\n    $cartItem2,\n    $cartItem3\n]);\nCart::storeItems($items);\n```\n\n### Quantity Management\n\n#### `increaseQuantity(CartItemContract $item, int $quantity = 1): static`\nIncreases the quantity of a specific cart item.\n\n```php\n// Increase by 1 (default)\nCart::increaseQuantity($cartItem);\n\n// Increase by specific amount\nCart::increaseQuantity($cartItem, 5);\n```\n\n**Throws:**\n- `NotFoundException` - if item doesn't exist in cart\n\n#### `decreaseQuantity(CartItemContract $item, int $quantity = 1): Driver`\nDecreases the quantity of a specific cart item.\n\n```php\n// Decrease by 1 (default)\nCart::decreaseQuantity($cartItem);\n\n// Decrease by specific amount\nCart::decreaseQuantity($cartItem, 3);\n```\n\n**Throws:**\n- `NotFoundException` - if item doesn't exist in cart\n\n### Item Removal\n\n#### `removeItem(CartItemContract $item): Driver`\nRemoves a specific item from the cart completely.\n\n```php\nCart::removeItem($cartItem);\n```\n\n#### `emptyCart(): static`\nRemoves all items from the cart.\n\n```php\nCart::emptyCart();\n```\n\n### Price Calculations\n\n#### `getItemPrice(CartItemContract $item, bool $incTaxes = true): string`\nCalculates the total price for a cart item (price × quantity). Returns a string for precision.\n\n```php\n// With taxes (default)\n$totalPrice = Cart::getItemPrice($cartItem);\n\n// Without taxes\n$totalPrice = Cart::getItemPrice($cartItem, false);\n```\n\n#### `getItemPricePerUnit(CartItemContract $item, bool $incTaxes = true): string`\nGets the price per single unit of a cart item.\n\n```php\n// With taxes (default)\n$unitPrice = Cart::getItemPricePerUnit($cartItem);\n\n// Without taxes\n$unitPrice = Cart::getItemPricePerUnit($cartItem, false);\n```\n\n#### `getTotalPrice(bool $incTaxes = true): string`\nCalculates the total price of all items in the cart.\n\n```php\n// With taxes (default)\n$total = Cart::getTotalPrice();\n\n// Without taxes\n$total = Cart::getTotalPrice(false);\n```\n\n### Usage Examples\n\n#### Basic Cart Operations\n```php\nuse Isapp\\LaravelCart\\Facades\\Cart;\nuse App\\Models\\Product;\nuse Isapp\\LaravelCart\\Models\\CartItem;\n\n// Create and add item to cart\n$product = Product::find(1);\n$cartItem = new CartItem();\n$cartItem-\u003eitemable()-\u003eassociate($product);\n\nCart::storeItem($cartItem);\n\n// Get cart with items\n$cart = Cart::get();\necho \"Items in cart: \" . $cart-\u003eitems-\u003ecount();\n\n// Calculate totals\n$total = Cart::getTotalPrice();\necho \"Cart total: $\" . $total;\n```\n\n#### Working with Different Users\n```php\n// Admin adding items to user's cart\n$user = User::find(123);\n$product = Product::find(1);\n$cartItem = new CartItem();\n$cartItem-\u003eitemable()-\u003eassociate($product);\n\nCart::setUser($user)-\u003estoreItem($cartItem);\n```\n\n#### Quantity Management\n```php\n// Get existing cart item\n$cart = Cart::get();\n$cartItem = $cart-\u003eitems-\u003efirst();\n\n// Increase quantity\nCart::increaseQuantity($cartItem, 2);\n\n// Decrease quantity\nCart::decreaseQuantity($cartItem, 1);\n\n// Remove item completely\nCart::removeItem($cartItem);\n```\n\n#### Price Calculations\n```php\n$cart = Cart::get();\n\nforeach ($cart-\u003eitems as $item) {\n    $unitPrice = Cart::getItemPricePerUnit($item);\n    $totalItemPrice = Cart::getItemPrice($item);\n    \n    echo \"Unit: $unitPrice, Total: $totalItemPrice\\n\";\n}\n\n$grandTotal = Cart::getTotalPrice();\necho \"Grand Total: $grandTotal\";\n```\n\n### Error Handling\n\nThe DatabaseDriver throws specific exceptions for different error conditions:\n\n```php\nuse Isapp\\LaravelCart\\Exceptions\\NotFoundException;\nuse Isapp\\LaravelCart\\Exceptions\\NotImplementedException;\nuse Isapp\\LaravelCart\\Exceptions\\ItemAssociatedWithDifferentCartException;\n\ntry {\n    Cart::increaseQuantity($cartItem, 5);\n} catch (NotFoundException $e) {\n    // Item not found in cart\n    return response()-\u003ejson(['error' =\u003e 'Item not found'], 404);\n} catch (NotImplementedException $e) {\n    // Itemable doesn't implement required interface\n    return response()-\u003ejson(['error' =\u003e 'Invalid item'], 400);\n} catch (ItemAssociatedWithDifferentCartException $e) {\n    // Trying to add item from another cart\n    return response()-\u003ejson(['error' =\u003e 'Item belongs to different cart'], 400);\n}\n```\n\n### Notes\n\n- All price calculations use BCMath for high precision arithmetic\n- Prices are returned as strings to maintain precision\n- The driver supports both authenticated users and guest sessions\n- Method chaining is supported for fluent interface\n- Cart items must implement `CartItemProduct` interface\n- The driver uses eager loading to optimize database queries\n\n## Adding a Custom Driver to Laravel Facade via Extend\n\nIf you need to add a custom driver to the `Cart` facade, you can do so by extending it within a service provider.\nHere is an example demonstrating how to achieve this:\n\n1. Create a custom driver class, for example:\n\n    ```php\n    namespace App\\Services;\n\n    use Isapp\\LaravelCart\\Contracts\\Driver;\n\n    class CustomCartDriver implements Driver\n    {\n        // Implement methods as per the contract and your needs\n        public function storeItem(CartItemContract $item): Driver\n        {\n            // Custom implementation\n        }\n\n        public function increaseQuantity(CartItemContract $item, int $quantity = 1): static\n        {\n            // Custom implementation\n        }\n\n        // Other required methods...\n    }\n    ```\n\n2. Register the custom driver in a service provider:\n\n    ```php\n    namespace App\\Providers;\n\n    use Illuminate\\Support\\ServiceProvider;\n    use Isapp\\LaravelCart\\Facades\\Cart;\n\n    class CartServiceProvider extends ServiceProvider\n    {\n        public function boot()\n        {\n            Cart::extend('custom', function () {\n                return new \\App\\Services\\CustomCartDriver;\n            });\n        }\n    }\n    ```\n## ToDo List\n- [ ] Add automated cleanup for expired cart sessions\n- [ ] Add method to get total items count in cart\n- [ ] Add method to get total quantity of all items\n- [x] Add method to find item by ID\n- [x] Add method to check if specific item exists in cart\n- [ ] Add stock validation before adding items\n- [ ] Add events firing (ItemAdded, ItemRemoved, etc.)\n- [ ] Add method to merge carts (guest → user)\n\n## Contributing\n\nContributions are welcome! If you have suggestions for improvements, new features, or find any issues, feel free to\nsubmit a pull request or open an issue in this repository.\n\nThank you for helping make this package better for the community!\n\n## License\n\nThis project is open-sourced software licensed under the [MIT License](https://opensource.org/licenses/MIT).\n\nYou are free to use, modify, and distribute it in your projects, as long as you comply with the terms of the license.\n\n---\n\nMaintained by [ISAPP](https://isapp.be) and [ISAP OÜ](https://isap.me).  \nCheck out our software development services at [isapp.be](https://isapp.be).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisap-ou%2Flaravel-cart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fisap-ou%2Flaravel-cart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisap-ou%2Flaravel-cart/lists"}