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

The Credit model ships query scopes for reporting over the ledger:

use RoundlyConsulting\Credits\Models\Credit;

Credit::query()->grants()->get();                               // amount > 0
Credit::query()->deductions()->get();                           // amount < 0
Credit::query()->upTo(now()->subWeek())->get();                 // created_at <= $at
Credit::query()->forCreditable($user)->get();                   // rows owned by $user
Credit::query()->bucket('promotional')->get();                  // rows in a named bucket
Credit::query()->buckets(['promotional', 'purchased'])->get();  // rows in any listed bucket
ScopeFilters to
grants()amount > 0
deductions()amount < 0
upTo($at)created_at <= $at
forCreditable($model)Rows owned by the given model (whereMorphedTo).
bucket($name)Rows in one named bucket.
buckets($names)Rows in any of the listed buckets.

Composing scopes

Scopes chain with each other and with the creditable’s credits() relation:

// Scopes compose — and work on the relation too.
Credit::query()->forCreditable($user)->bucket('promotional')->grants()->sum('amount');

$user->credits()->deductions()->upTo(now()->subMonth())->latest()->get();

The model casts amount to int and meta to array, and exposes the owner through the creditable() MorphTo relation.

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.