a multi driver shopping cart package
- Run the command below to add this package:
composer require laravelir/cart
- Open your config/app.php and add the following to the providers/aliases array:
Laravelir\Cart\Providers\CartServiceProvider::class,
'Cart' => Laravelir\Cart\Facade\Cart::class,
- Run the command below to install package:
php artisan cart:install
Session - Cookie - Eloquent - Cache
you can set your favorite driver in config file
eloquent driver need to authenticated user for save user's id
add HasCart
trait to User model
this trait has one method:
$this->cart(); // return cart
$cart = resolve(Cart());
$cart->all();
$cart->add();
$cart->has();
$cart->update();
$cart->count();
$cart->delete();
$cart->truncate();
cart();
cartItems();
Cart::user($user_id)->add(); Cart::driver('session')->add(); Cart::add(['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => ['size' => 'large']]);
$user = auth()->user();
$product = Product::first();
$cart = resolve(Cart());
$data = [ 'item' => $product, 'title' => $product->title, 'quantity' => 1, 'price' => $product->price, ];
$options = [ 'color' => 'red', 'size' => 'XL' ]; $user->cart->add($data, $options);
$data = [
]; Cart::update($itemId, $data); Cart::update($itemId)->increaseQuantity(); Cart::update($itemId)->decreaseQuantity(); Cart::all(); Cart::count(); Cart::get($itemId); Cart::delete($itemId); Cart::truncate();
For the convenience of faster adding items to cart and their automatic association, your model can implement Buyable interface. To do so, it must implement such functions:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product exends Model implements Cartable {
public function getCartableId() {
return $this->id;
}
public function getCartableTitle() {
return $this->title;
}
public function getCartablePrice() {
return $this->price;
}
}
The cart has following events:
cart.added - when an item added cart.updated - when an item cart updated cart.deleted - when an item deleted
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.