Polyscope - The agent-first dev environment for Laravel

Laravel Cart

binafy/laravel-cart image

Laravel Cart stats

Downloads
41
Stars
3
Open Issues
0
Forks
0

View on GitHub →

Laravel Card is designed to be highly customizable and flexible, making it easy to adapt to your specific use case.

Laravel Cart

binafy-laravel-cart

Introduction

The Laravel Cart is a highly customizable and flexible package that integrates basket functionality into your Laravel application. It simplifies storing and managing cart items, supporting multiple item types and quantities. It is ideal for e-commerce platforms to create carts, attach items, and manage them efficiently. Installation is straightforward via Composer, and it offers robust features like secure item storage, easy cart manipulation, and seamless integration with your existing Laravel app.

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

Installation

You can install the package with Composer:

composer require binafy/laravel-cart

Publish

If you want to publish a config file, you can use this command:

php artisan vendor:publish --tag="laravel-cart-config"

If you want to publish the migrations, you can use this command:

php artisan vendor:publish --tag="laravel-cart-migrations"

For convenience, you can use this command to publish config, migration, and ... files:

php artisan vendor:publish --provider="Binafy\LaravelCart\Providers\LaravelCartServiceProvider"

After publishing, run the php artisan migrate command.

Usage

Store Cart

For storing a new cart, you can use Cart model:

use \Binafy\LaravelCart\Models\Cart;
 
$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);

Store Items For a Cart

If you want to store items for cart, first you need to create a cart and attach items to 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);

If you may to access the items of one cart, you can use items relation that exists in Cart model.

There is no need to use any Interface or something for itemable.

Access Itemable

If you want to access to itemable in CartItem, you can use itemable relation:

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

Create Cart With Storing Items

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

Store multiple items

If you may to store multiple items for a cart, you can use storeItems method:

$items = [
[
'itemable' => $product1,
'quantity' => 2,
],
[
'itemable' => $product2,
'quantity' => 1,
],
[
'itemable' => $product3,
'quantity' => 5,
],
];
 
$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
$cart->storeItems($items);

Contributors

Thanks to all the people who contributed. Contributors.

Security

If you discover any security-related issues, please email binafy23@gmail.com instead of using the issue tracker.

Changelog

The changelog can be found in the CHANGELOG.md file of the GitHub repository. It lists the changes, bug fixes, and improvements made to each version of the Laravel User Monitoring package.

License

The MIT License (MIT). Please see License File for more information.

binafy photo

We are helping to open-source world

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Binafy Laravel Cart Related Articles

JSON:API Resource in Laravel 12.45 image

JSON:API Resource in Laravel 12.45

Read article
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge