### Install Laravel Cart using Composer Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md This command installs the Laravel Cart package using Composer, a dependency manager for PHP. It downloads and integrates the necessary files into your Laravel project. ```bash composer require binafy/laravel-cart ``` -------------------------------- ### Creating a Cart and Storing Items Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md Demonstrates how to create a cart and store items efficiently using `firstOrCreateWithStoreItems`. This method requires the item model to implement the `Cartable` interface and define a `getPrice` method. ```php use Binafy\LaravelCart\Cartable; use Illuminate\Database\Eloquent\Model; class Product extends Model implements Cartable { public function getPrice(): int { // } } // Storing a single item Cart::query()->firstOrCreateWithStoreItems( item: $product, quantity: 1, userId: $user->id ); ``` -------------------------------- ### Using Laravel Cart Facade with Drivers Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md Demonstrates how to utilize the Laravel Cart facade to store and remove items, with an option to specify a driver. The default driver is 'database', but 'session' can also be used. Configuration for drivers is available in 'config/laravel-cart.php'. ```php storeItem($item, $userId|null); LaravelCart::storeItem($item, $userId|null); LaravelCart::driver('database')->storeItem($item, $userId|null); LaravelCart::driver('session')->removeItem($item); ``` -------------------------------- ### Storing a New Cart with Laravel Cart Model Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md Shows how to create a new cart entry using the `Cart` model, ensuring a cart exists for a given user. This is a prerequisite for adding items to a cart. ```php use \Binafy\LaravelCart\Models\Cart; $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); ``` -------------------------------- ### Publish Laravel Cart Configuration Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md These commands publish the configuration files, migration files, or all files associated with the Laravel Cart package. Publishing allows you to customize package settings and database schema. ```shell php artisan vendor:publish --tag="laravel-cart-config" ``` ```shell php artisan vendor:publish --tag="laravel-cart-migrations" ``` ```shell php artisan vendor:publish --provider="Binafy\LaravelCart\Providers\LaravelCartServiceProvider" ``` -------------------------------- ### Storing Multiple Items in a Cart Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md Explains how to add multiple items to an existing cart using the `storeItems` method. This method accepts an array of items, each with an 'itemable' and 'quantity' key. ```php use \Binafy\LaravelCart\Models\Cart; $items = [ [ 'itemable' => $product1, 'quantity' => 2, ], [ 'itemable' => $product2, 'quantity' => 1, ], [ 'itemable' => $product3, 'quantity' => 5, ], ]; $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); $cart->storeItems($items); ``` -------------------------------- ### Emptying All Items from a Cart Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md Shows how to remove all items from a cart by calling the `emptyCart` method. This effectively resets the cart to an empty state. ```php use \Binafy\LaravelCart\Models\Cart; $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); $cart->emptyCart(); ``` -------------------------------- ### Increasing Item Quantity in a Cart Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md Illustrates how to increase the quantity of a specific item within a cart using the `increaseQuantity` method. The method accepts the item and the quantity to add (defaults to 1). ```php use \Binafy\LaravelCart\Models\Cart; $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); $cart->increaseQuantity(item: $item, quantity: 2); // By default quantity is 1 ``` -------------------------------- ### Removing an Item from a Cart Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md Demonstrates how to remove a specific item from a cart using the `removeItem` method. This method takes the item to be removed as an argument. ```php use \Binafy\LaravelCart\Models\Cart; $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); $cart->removeItem($product1); ``` -------------------------------- ### Storing a Single Item in a Cart Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md Provides two methods for storing a single item in a cart: directly saving a `CartItem` instance or using the convenient `storeItem` method which accepts either a model or an array. ```php use \Binafy\LaravelCart\Models\Cart; use \Binafy\LaravelCart\Models\CartItem; $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); // Saving a CartItem instance $cartItem = new CartItem([ 'itemable_id' => $itemable->id, 'itemable_type' => $itemable::class, 'quantity' => 1, ]); $cart->items()->save($cartItem); // Using storeItem method with a model $cart->storeItem($itemable); // Using storeItem method with an array $item['itemable'] = $itemable; $item['quantity'] = 1; $cart->storeItem($item); ``` -------------------------------- ### Decreasing Item Quantity in a Cart Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md Demonstrates how to decrease the quantity of a specific item within a cart using the `decreaseQuantity` method. The method accepts the item and the quantity to subtract (defaults to 1). ```php use \Binafy\LaravelCart\Models\Cart; $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); $cart->decreaseQuantity(item: $item, quantity: 2); // By default quantity is 1 ``` -------------------------------- ### Accessing Itemable Relation in CartItem Source: https://github.com/binafy/laravel-cart/blob/1.x/README.md Illustrates how to access the related itemable model from a `CartItem` instance using the `itemable` relation. This is useful for retrieving details of the item stored in the cart. ```php use \Binafy\LaravelCart\Models\CartItem; $cartItem = new CartItem([ 'itemable_id' => $itemable->id, 'itemable_type' => $itemable::class, 'quantity' => 1, ]); $cartItem->itemable()->first(); // Return Model Instance ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.