composer require isapp/laravel-cart and publish the config with php artisan vendor:publish --provider="Isapp\LaravelCart\CartServiceProvider" --tag="config".use Isapp\LaravelCart\Facades\Cart;
// Add an item (e.g., a product model)
Cart::add([
'id' => 1,
'name' => 'Product Name',
'price' => 19.99,
'quantity' => 1,
]);
$cart = Cart::get();
$items = $cart->items;
$total = $cart->total;
config/laravel-cart.php (storage driver, session/DB settings).Isapp\LaravelCart\Facades\Cart (primary API entry point).php artisan vendor:publish --tag="migrations" (if using DB storage).Cart::add($item)->associate('App\Models\Product')->save();
Cart::update(1, ['quantity' => 2]);
Cart::remove(1);
Cart::clear(); // Removes all items
Cart::empty(); // Resets cart but keeps session
config/laravel-cart.php:
'driver' => 'database',
'table' => 'cart_items',
Sync carts between sessions/DB with:
Cart::sync();
Cart::add($product)->associate(Product::class);
$cartItems = Cart::items()->where('quantity', '>', 1)->get();
event(new UserLoggedIn($user)); // Trigger sync
Configure in config/laravel-cart.php:
'sync_on_login' => true,
'user_model' => App\Models\User::class,
$subtotal = Cart::subtotal();
$tax = Cart::tax(0.1); // 10% tax
$total = Cart::total();
config/laravel-cart.php:
'currency' => 'USD',
'precision' => 2,
public function addToCart(Request $request, Product $product) {
Cart::add($product)->save();
return back()->with('success', 'Added to cart!');
}
return response()->json(Cart::get());
@inject('cart', 'Isapp\LaravelCart\Facades\Cart')
<div>Items: {{ $cart->count() }}</div>
cart helper for concise syntax:
@cart(['item' => $cart->items])
Session vs. Database Sync:
sync_on_login is true, ensure the UserLoggedIn event is fired (or manually call Cart::sync()).config/laravel-cart.php for sync_on_login and verify event listeners.Model Association Mismatches:
id matches the cart item’s id. Use associate() before save():
Cart::add($product)->associate(Product::class)->save();
ModelNotFoundException if the associated model doesn’t exist.Precision Issues:
Cart::precision() to enforce decimal places:
Cart::precision(4); // For cents
precision in config/laravel-cart.php.Guest Cart Persistence:
Migration Conflicts:
cart_items tables to avoid conflicts. Run php artisan migrate after publishing.\Log::info('Cart:', ['items' => Cart::get()->items]);
session()->all().cart_items table directly:
SELECT * FROM cart_items WHERE user_id IS NULL; -- Guest carts
Custom Storage:
Isapp\LaravelCart\Contracts\CartStorage interface to support Redis or other drivers.Event Hooks:
cart.item.added) to trigger notifications or analytics:
Cart::on('item.added', function ($item) {
// Send email or log event
});
Validation:
use Isapp\LaravelCart\Exceptions\ValidationException;
try {
Cart::add($item)->validate(function ($item) {
return $item['quantity'] > 0;
});
} catch (ValidationException $e) {
// Handle error
}
Testing:
Cart facade in tests:
$this->mock(Isapp\LaravelCart\Facades\Cart::class)->shouldReceive('get')->andReturn($mockCart);
Cart::flush() to reset test carts.Performance:
$cart = Cart::items()->with('product')->get();
How can I help you explore Laravel packages today?