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 traitWhat the trait adds
| Method | Returns | Purpose |
|---|---|---|
credits() | MorphMany | Every ledger row owned by the entity. |
modifyCredits($amount, $description, $meta, $allowOverdraft, $bucket) | Credit | Append a grant (positive) or deduction (negative). |
setCreditsTo($amount, $description, $meta, $allowOverdraft, $bucket) | ?Credit | Record the delta to an exact balance; null when nothing changes. |
creditsBalance($at, $bucket) | int | One bucket’s balance, optionally as of a point in time. |
hasCredits($amount = 1, $at, $bucket) | bool | Balance is at least $amount. |
creditsBalanceForBuckets($buckets, $at) | int | Sum of the listed buckets. |
totalCreditsBalance($at) | int | Sum of every bucket. |
displayCredits($amount, $scale, $rounding) | string | Any integer amount as a locale-free decimal string. |
displayCreditsBalance($bucket, $scale, $rounding, $at) | string | A bucket’s balance as a locale-free decimal string. |
creditsCurrency($bucket) | ?Currency | The bucket’s money currency, or null for plain credits. |
creditsBalanceMoney($bucket, $at) | Money | A denominated bucket’s balance as Money. |
modifyCreditsMoney($amount, $description, $meta, $allowOverdraft, $bucket) | Credit | Grant or deduct a Money of the bucket’s currency. |
formatCreditsBalance($bucket, $locale) | string | A 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 cryptoBy 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.