NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Credits for Laravel

Creditable models

Implement the Creditable interface and use the HasCredits trait on any Eloquent model. The trait provides every method the interface declares:

use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Credits\Interfaces\Creditable;
use RoundlyConsulting\Credits\Traits\HasCredits;

final class User extends Model implements Creditable
{
    use HasCredits;
}

Implement the interface — don’t skip it. Every API takes the owner as a Model&Creditable, so a model that only uses the trait fails with a TypeError on its first credits call (static analysis flags it before that), and credits:modify skips any entity that is not Creditable.

Any model, not just users

Credits attach polymorphically, so teams, organisations, projects or devices can each hold their own balance in the same table:

final class Team extends Model implements Creditable
{
    use HasCredits;
}

Credits::for($team)->add(5000, 'prepaid API calls');
Credits::for($team)->balance();         // 5000
$team->creditsBalance();                // 5000 — the same read through the trait

What the trait adds

MethodReturnsPurpose
credits()MorphManyEvery ledger row owned by the entity.
modifyCredits($amount, $description, $meta, $allowOverdraft, $bucket)CreditAppend a grant (positive) or deduction (negative).
setCreditsTo($amount, $description, $meta, $allowOverdraft, $bucket)?CreditRecord the delta to an exact balance; null when nothing changes.
creditsBalance($at, $bucket)intOne bucket’s balance, optionally as of a point in time.
hasCredits($amount = 1, $at, $bucket)boolBalance is at least $amount.
creditsBalanceForBuckets($buckets, $at)intSum of the listed buckets.
totalCreditsBalance($at)intSum of every bucket.
displayCredits($amount, $scale, $rounding)stringAny integer amount as a locale-free decimal string.
displayCreditsBalance($bucket, $scale, $rounding, $at)stringA bucket’s balance as a locale-free decimal string.
creditsCurrency($bucket)?CurrencyThe bucket’s money currency, or null for plain credits.
creditsBalanceMoney($bucket, $at)MoneyA denominated bucket’s balance as Money.
modifyCreditsMoney($amount, $description, $meta, $allowOverdraft, $bucket)CreditGrant or deduct a Money of the bucket’s currency.
formatCreditsBalance($bucket, $locale)stringA denominated bucket’s balance through money’s locale formatter.

Every read and write accepts an optional bucket; omit it and the configured default_bucket is used. Every read accepts an optional point in time ($at, any CarbonInterface). Each method is shorthand for the same call through Credits::for($this) — see The Credits facade.

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.