PHP code example of binafy / laravel-cart

1. Go to this page and download the library: Download binafy/laravel-cart library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

binafy / laravel-cart example snippets




use Binafy\LaravelCart\LaravelCart;

LaravelCart::driver('session')->storeItem($item, $userId|null);
LaravelCart::storeItem($item $userId|null);



use Binafy\LaravelCart\LaravelCart;

LaravelCart::driver('database')->storeItem($item, $userId|null);
LaravelCart::driver('session')->removeItem($item);

use \Binafy\LaravelCart\Models\Cart;

$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);

$cartItem = new CartItem([
    'itemable_id' => $itemable->id,
    'itemable_type' => $itemable::class,
    'quantity' => 1,
]);

$cartItem->itemable()->first(); // Return Model Instance

use Binafy\LaravelCart\Cartable;

class Product extends Model implements Cartable
{
    public function getPrice(): int
    {
        //
    }
}

Cart::query()->firstOrCreateWithStoreItems(
    item: $product,
    quantity: 1,
    userId: $user->id
);

$items = [
    [
        'itemable' => $product1,
        'quantity' => 2,
    ],
    [
        'itemable' => $product2,
        'quantity' => 1,
    ],
    [
        'itemable' => $product3,
        'quantity' => 5,
    ],
];

$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
$cart->storeItems($items);

$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
$cartItem = new CartItem([
    'itemable_id' => $itemable->id,
    'itemable_type' => $itemable::class,
    'quantity' => 1,
]);

$cart->items()->save($cartItem);

$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);

// Model
$cart->storeItem($itemable);

// Array
$item['itemable'] = $itemable;
$item['quantity'] = 1;
$cart->storeItem($item);

$items = [
    [
        'itemable' => $product1,
        'quantity' => 2,
    ],
    [
        'itemable' => $product2,
        'quantity' => 1,
    ],
    [
        'itemable' => $product3,
        'quantity' => 5,
    ],
];

$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
$cart->removeItem($product1);

$items = [
    [
        'itemable' => $product1,
        'quantity' => 2,
    ],
    [
        'itemable' => $product2,
        'quantity' => 1,
    ],
    [
        'itemable' => $product3,
        'quantity' => 5,
    ],
];

$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
$cart->emptyCart();

$cart->increaseQuantity(item: $item, quantity: 2); // By default quantity is 1

$cart->decreaseQuantity(item: $item, quantity: 2); // By default quantity is 1
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"