Laravel Packages

A Lightweight Cart Package for Laravel

Published
A Lightweight Cart Package for Laravel image

The binafy/laravel-cart package adds the ability to add shopping cart functionality to Laravel applications. It simplifies storing and managing cart items, supports storing multiple item types, and more:

Features:

  • Secure card information storage and management
  • Support for multiple payment gateways
  • Recurring payment and subscription management
  • Robust validation and error handling
  • Highly customizable and flexible architecture

From the package's documentation, here's an example of retrieving a cart for a given user and adding an item to the cart:

$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);
 
// Or create and store
Cart::query()->firstOrCreateWithStoreItems(
item: $product,
quantity: 1,
userId: $user->id
);

This package also allows you to store multiple items in the cart, and the cart items are polymorphic model associations. You can access the underlying model associated with the CartItem using the itemable() method:

$cartItem->itemable()->first();

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Related: GetCandy E-commerce Package for Laravel

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article