---
title: "Creditable models — Credits for Laravel | Roundly"
description: "Implement Creditable and use HasCredits on any Eloquent model — users, teams, accounts — to give it a ledger and a balance."
url: https://roundly-consulting.com/open-source/docs/credits-for-laravel/creditable-models
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Credits for Laravel](https://roundly-consulting.com/open-source/docs/credits-for-laravel.md)

# Creditable models

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

```php
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:

```php
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

| 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 crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## 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.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/credits-for-laravel.md)
