roundly-consulting / credits-for-laravel
Manage credits / wallet balance on entity.
Package info
github.com/roundly-consulting/credits-for-laravel
pkg:composer/roundly-consulting/credits-for-laravel
Fund package maintenance!
Requires
- php: ^8.4
- ext-bcmath: *
- illuminate/contracts: ^12.0|^13.0
- roundly-consulting/money-for-laravel: ^1.0
- roundly-consulting/package-toolkit-for-laravel: ^1.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- nunomaduro/collision: ^8.8|^9.0
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- roundly-consulting/testing-for-laravel: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Credits for Laravel
Credits and wallet balances on any Eloquent model. Every change is an immutable, time-stamped ledger row, so a balance is always the sum of its history — readable at any point in time, split into named buckets, and protected against overdraft and race conditions.
Installation
Requires PHP 8.4 (ext-bcmath), Laravel 12 or 13.
composer require roundly-consulting/credits-for-laravel
php artisan vendor:publish --tag="credits-migrations"
php artisan migrate
If your creditable models have UUID/ULID keys, set CREDITS_KEY_TYPE before migrating.
Usage
Make any model creditable:
use Illuminate\Database\Eloquent\Model; use RoundlyConsulting\Credits\Interfaces\Creditable; use RoundlyConsulting\Credits\Traits\HasCredits; final class User extends Model implements Creditable { use HasCredits; }
Then grant, spend and read its credits:
use RoundlyConsulting\Credits\Facades\Credits; Credits::for($user)->add(100, 'Welcome'); // a ledger row Credits::for($user)->deduct(30, 'Order #42', ['order_id' => 42]); // InsufficientCreditsException below the floor Credits::for($user)->balance(); // 70 Credits::for($user)->balance(now()->subWeek()); // as of a point in time Credits::for($user)->has(50); // true $points = Credits::for($user)->bucket('points'); // an independent pool $points->add(500, 'Loyalty bonus'); $points->balance(); // 500
Documentation
The full documentation — configuration, every feature and its API, and testing — lives on our website: roundly-consulting.com/open-source/docs/credits-for-laravel
Release notes are in CHANGELOG.md. To contribute, see the contributing guide.
Support our work
This package is free and open source, built and maintained by Roundly Consulting. If it saves you time, please consider supporting our open-source work — a one-time donation, a monthly pledge on Patreon or a crypto donation helps fund maintenance, new features and new packages.
License
The MIT License (MIT). Please see License File for more information.