A Lightweight Cart Package for Laravel

  • Published on 01 July, 2024
  • Words: 148

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

A Lightweight Cart Package for Laravel

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();

 

Here you can see an example of how it works.

 

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

Antonio Jenaro
Antonio Jenaro

Web Developer

Source: Laravel news

Start the conversation

Become a member of Antonio Jenaro to start commenting.

Sign up now

Already a member? Sign in